Ask your question here! Try to give as many details as possible
Hi everyone,
i am making a 2d platformer; while playing an animation (“attack”) it run another animation (“idle”) without fully run the previous animation…Mmmm, i thougth a way so solve this probblem is defining come flag variable to indicate wn animation is already running…what do you think about ?
ok, considering I defined this to play the several animations:
var animation_player: AnimationPlayer
Could I check if no one animation is being played??
Yes, as @paintsimmon mentioned you can use is_playing:
if not animation_player.is_playing():
# do-stuff
Or you use current_animation to get the currently playing animation (it’s "" when none is playing). Or you connect/await the animation_finished signal as @TokyoFunkScene suggested and start the next animation when the previous is finished.
Or you use queue to queue the next animation.
do not use an AnimationPlayer on its own unless it’s for something very simple, and in that case an AnimatedSprite would be better.
explosions, single looped animations = AnimatedSprite
characters, enemies, a chest, a door = AnimationPlayer with AnimationTree
Your other question was about setting up collisions? and I told you to use a state machine?
well with an AnimationTree you could define in what order you want the animations to run, or if an animation must not transition at all, like a death animation.
idle -> hurt -> death
idle -> electricity -> death
you can also make them go back when a condition is met, like with walking:
idle -> walk
walk -> idle
an AnimationTree will also ensure that an animation finishes playing before going into the next one, and you can set it to smoothly transition, blending frames from one with the next.
Looking at the code, Input.is_action_just_pressed() only checks for 1 frame of the specified input, so it’s probably only playing the attack animation for 1 frame, because it’s nested in a bunch of if/elif/else statements.
In other words, the condition of the attack button being pressed is only true for one frame, so the animation plays for one frame then the idle animation begins to play again.
You may have to rewrite the if conditions so they play properly, and maybe throw in an await() command (or use a Timer Node) to make sure the attack animation finishes.
And of course, here is the obligatory “Have you tried using a State Machine” question, it can help compartmentalize your animations as well as give you modular control over each individual action.
You should make your own post so that you can mark the solution that helped you and help people who come after you. By extending a post by someone else, you cannot do this.