Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | mattpsd |
I want to play a sound based on the frame of animation and I want it to play only once when the frame is first introduced. I got it working like this:
var stepPlayed = false
func stateRun():
$AnimatedSprite.animation = "Run"
if $AnimatedSprite.frame == 0 or $AnimatedSprite.frame == 4:
if StepPlayed == false:
$AudioStep.play()
stepPlayed = true
elif $AnimatedSprite.frame != 0 and $AnimatedSprite.frame != 4:
stepPlayed = false
I believe this isn’t the most efficient way to do it since it can create tons of variables. Is there a way to solve it with declaring a variable in the scope of the stateRun() function? Or any even better ways to do it?
I know the problem can be approached with the AnimationPlayer node, but I don’t really like this solution because it creates a bunch of other problems.
And I sometimes encounter similar problems when coding not only with sound and animations, but generally with variables that need to be changed at the end of _physics_process(delta) and their state is read in the beginning of _physics_process(delta).