0 votes

So I'm creating a simple 2D breakout game and I have instanced the bricks in code. I created somewhat of a grid of bricks but the problem is that when the ball hits any one of the bricks then all the bricks are gone/deleted.

func spawn_bricks(row,column):
var pos_x = 64
var pos_y = 0
#column
for i in range(row):
    pos_y += 32
    pos_x = 64
    #row
    for j in range(column):
        brick = BRICK_SCENE.instance()
        brick.add_to_group("Bricks")
        brick.position = Vector2(pos_x, pos_y)
        pos_x += 64
        add_child(brick)

#Rigidbody2D Ball uses signal body_entered and deletes brick  
func _on_Ball_body_entered(body):
    if body.is_in_group("Bricks"):
        call_deferred("free")

The bricks are ordered in rows and columns so 'spawn_bricks(3,5)' would spawn 3 rows of 5 bricks. Is there any way where if the ball collided with one brick then that brick is deleted and not the whole grid.

in Engine by (20 points)

1 Answer

0 votes
Best answer

Your code is deleting the brick manager, not the bricks. Try changing the code to

func _on_Ball_body_entered(body):
    if body.is_in_group("Bricks"):
        body.call_deferred("free")

You need to get the individual brick to run the free code.

by (449 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.