Reputation: 13
I am currently working on an enemy spawner for a 2D arena wave spawner game in Godot.
I have an export(resource) variable that allows me to insert a spawning animation into the scene.
My question is: How do I play said animation?
I have created comments in my code that explains how it is set up. The two possible ways I think it could be solved is either by
Thanks!
extends Node2D
export(NodePath) var node_path
const WIDTH = 254
const HEIGHT = 126
export(Resource) var ENEMY
export(Resource) var SPAWNANIMATION
var spawnArea = Rect2()
var delta = 3
var offset = 0.5
#Creates the spawnArea and randomizes positions for the enemy to spawn
func _ready():
randomize()
spawnArea = Rect2(0, 0, WIDTH, HEIGHT)
setnextspawn()
#Spawns an Enemy at said Random position
func spawnEnemy():
var position = Vector2(randi()%WIDTH, randi()%HEIGHT)
var enemy = ENEMY.instance()
enemy.position = position #Determined position
#EnemySpawningAnim(position)
get_node(node_path).add_child(enemy)
return position
#Spawn Timer for in between each enemy spawn
func setnextspawn():
var nextTime = delta + (randf()-0.5) * 2 * offset
$Timer.wait_time = nextTime
$Timer.start()
func _on_Timer_timeout():
spawnEnemy()
setnextspawn()
#Function that takes a position to play the animation at
func EnemySpawningAnim(position):
pass
Upvotes: 1
Views: 366
Reputation: 3500
You'll need an AnimationPlayer
node that you add_animation()
resource when _ready()
. Here's a code example:
func _ready():
$AnimationPlayer.add_animation('spawn', SPAWNANIMATION)
func EnemySpawningAnim(position):
# ... logic to handle position
$AnimationPlayer.play('spawn')
Upvotes: 0