Menu
CoddyTech

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 icon

Challenge

Hard

Make big rocks split and small rocks break.

  1. In rock.gd, add var size = 2 and set scale = Vector2.ONE * size * 0.5 in _ready.
  2. Give spawn_rock a size, and set rock.size before add_child. The first 4 get 2.
  3. Split only when rock.size == 2, into two of size 1.

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

Practice on your own: Godot playground