June 2018
Beginner to intermediate
298 pages
7h 38m
English
When you fly around, the player ship bounces off the rocks, because both bodies are RigidBody2D nodes. However, if you want to make something happen when two rigid bodies collide, you need to enable contact monitoring. Select the Player node and in the Inspector, set Contact Monitoring to On. By default, no contacts are reported, so you must also set Contacts Reported to 1. Now, the body will emit a signal when it contacts another body. Click on the Node tab and connect the body_entered signal:
func _on_Player_body_entered( body ): if body.is_in_group('rocks'): body.explode() $Explosion.show() $Explosion/AnimationPlayer.play("explosion") self.lives -= 1 if lives <= 0: change_state(DEAD) else: change_state(INVULNERABLE) ...Read now
Unlock full access