Big and Small Rocks
Part of the Projects section of Coddy's Godot journey. Lesson 19 of 36.
Only big rocks should split. Give each rock a size, 2 for big and 1 for small, and draw it from that in rock.gd's _ready: scale = Vector2.ONE * size * 0.5.
spawn_rock takes the size too. Set rock.size before add_child(rock): _ready runs inside add_child, so a size set after it comes too late for the scale.
func _on_rock_broken(rock):
if rock.size == 2:
spawn_rock.call_deferred(rock.position, rock.velocity.rotated(0.8) * 1.5, 1)
spawn_rock.call_deferred(rock.position, rock.velocity.rotated(-0.8) * 1.5, 1)A small rock just breaks.
Challenge
HardMake big rocks split and small rocks break.
- In
rock.gd, addvar size = 2and setscale = Vector2.ONE * size * 0.5in_ready. - Give
spawn_rockasize, and setrock.sizebeforeadd_child. The first 4 get2. - Split only when
rock.size == 2, into two of size1.
Press Play and shoot: big rocks split, small ones break. Then Check.
Try it yourself
extends Node2D
const ROCK = preload("res://rock.tscn")
func _ready():
for i in 4:
spawn_rock(Vector2(randf_range(0, 640), 0), Vector2(randf_range(-90, 90), randf_range(40, 90)))
func spawn_rock(where, velocity):
var rock = ROCK.instantiate()
rock.position = where
rock.velocity = velocity
rock.broken.connect(_on_rock_broken)
add_child(rock)
func _on_rock_broken(rock):
spawn_rock.call_deferred(rock.position, rock.velocity.rotated(0.8) * 1.5)
spawn_rock.call_deferred(rock.position, rock.velocity.rotated(-0.8) * 1.5)
All lessons in Projects
2Project: Asteroid Shooter
Turn the ShipFire the EnginePractice on your own: Godot playground