Making a Sprite Explode in Godot: A Godot 4 Tutorial
Do you want to explode a game sprite? I know I do. So lets start setting this scene up.
Create a new project. In the scene tree add a Node2D and name it Main.
Attach a script. We will need it. Then save the scene, and try to play the scene. This will give you the option to select this scene as the "Main Scene" or entry point of the game.
Next we need to make a sprite we can blow up. Click "Scene" -> "New Scene".
We are going to create an Area2D and add two children nodes. A Sprite2D and a CollisionShape2D.
Select the Sprite2D and look in the Inspector dock. Look where it says "Texture <empty>". Click on the little arrow and select "Load..."
For this tutorial I chose to load "icon.svg"
You will notice a group of red dots in the 2D window. Click on these to expand your shape over the sprite.
Save the scene and name it something like "victim". In the scene tree pick the Area2D node and attach a script.
In the Inspector dock you will see a tab called "Node".
Double click on the input_event signal. Click "Connect". You should now see in the script a new function called _on_input_event. We will modify the script to this...
extends Area2D signal die var dying := false # Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void: if event is InputEventMouseButton and event.button_index == 1: emit_signal("die")
Our animation resource is ready and we can now create a new scene. Create a Node2D. Lets call it explode. Add an AnimatedSprite2D as a child.
Save the scene as "explode.tscn"
extends Node2D # Called when the node enters the scene tree for the first time. func _ready() -> void: $AnimatedSprite2D.play("default") $AnimatedSprite2D.animation_finished.connect(queue_free) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta: float) -> void: pass
extends Node2D @export var victim_scene: PackedScene @export var explosion_scene: PackedScene var victim : Node2D # Called when the node enters the scene tree for the first time. func _ready() -> void: pass # Replace with function body. # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass
extends Node2D @export var victim_scene: PackedScene @export var explosion_scene: PackedScene var victim : Node2D # Called when the node enters the scene tree for the first time. func _ready() -> void: victim = victim_scene.instantiate() victim.position = Vector2(200.0, 200.0) add_child(victim) #connect to victim's "die" signal if victim.has_signal("die"): victim.die.connect(_on_die) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass func _on_die() -> void: pass
extends Node2D @export var victim_scene: PackedScene @export var explosion_scene: PackedScene var victim : Node2D # Called when the node enters the scene tree for the first time. func _ready() -> void: randomize() victim = victim_scene.instantiate() victim.position = Vector2(200.0, 200.0) add_child(victim) #connect to victim's "die" signal if victim.has_signal("die"): victim.die.connect(_on_die) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass func _on_die() -> void: if victim.dying: return victim.dying = true victim.get_node("Sprite2D").visible = false for i in range(6): var explosion = explosion_scene.instantiate() explosion.scale *= randf_range(.25, 1.25) var offset = Vector2(randf_range(-30.0, 30.0), randf_range(-30.0, 30.0)) explosion.global_position = victim.global_position + offset add_child(explosion) await get_tree().create_timer(0.05).timeout await get_tree().create_timer(2.0).timeout if victim: victim.queue_free()
Comments
Post a Comment