Emit signal on certain frames in animated sprite

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kutkulio

Is it possible to emit the shoot signal on a certain frame of animation? I made a 20-odd frame animation of a minigun spooling up, shooting, then spooling down and I just want to time a burst in the middle. I’m new and pulled an example from this link: Reddit - Dive into anything about how to make something happen on an individual frame.

I’m getting the error “expected block in pattern branch” on the emit line at the bottom.

func _on_GunTimer_timeout():
	$AnimatedSprite.play("firing")
	$AnimatedSprite.connect("frame_changed", self, "on_firing_frame")
	$AnimatedSprite.connect("animation_finished", self, "_on_firing_animation_finished")
#	emit_signal('shoot', Bullet, $Position2D.global_position)
	$GunTimer.start()

func _on_firing_animation_finished():
	$AnimatedSprite.play("default")

func on_firing_frame():
    match frame:
        9, 11, 13, 15:
			emit_signal('shoot', Bullet, $Position2D.global_position)
:bust_in_silhouette: Reply From: Chafmere

Hi There,

My suggestion would be to switch from using the node Animated Sprite to using Animation player and animation tree player. You can use these two together to control the animations of your Sprite.

If you use the Animation player you can actually trigger functions on a frame by frame basis. This gives you substantially more control over what’s happening.

If you want a crash course on how to use the animation player and animation tree player nodes then you should watch heartbeast’s action RPG tutorial on youtube. He covers it really well. I was sceptical at first but once I put in the work and switched my code over to work with it, I’ve been able to do much more with my sprites.

Thanks so much. I’ll check that out!

Kutkulio | 2020-06-18 00:40