How do I make a 2D animation Stop looping?/ Play while Pressed?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kaboom1212
:warning: Old Version Published before Godot 3 was released.

I’ve done a fair amount of research on the scripting in this engine and have taught myself a whole bunch of different things.I am currently making my first program which is a simple platformer where a man hops around as well as runs around. For this project I made a series of pixel animations, with four specific animations, a Left idle (as in facing left), a Right idle, a Left walk cycle, and a right walk cycle. I have each of these animations keyed up and working in the animation player. I can run each one, but I now have to figure out a way to play each one. I made a basic code which I thought would work.
var move_speed = 150
var anim
func _process(delta):
.
anim = get_node(“AnimationPlayer”)
.
.
.
if Input.is_key_pressed(68): #68 = D
set_axis_velocity(Vector2(move_speed,0))
anim.play(“Right Walk”)

It’s a simple enough code, it handles walking and playing an animation at the same time. However, I didn’t realize that this code refreshes multiple times per second. So I looked up a few different solutions and figured out, the easiest way to go about play the animation is to only play something when the animation changes.

‘Ray Koopa’ - " As you check the input in _fixed_process, you call anim_player.play() several times a frame, which always seems to restart the animation, and thus, keeps the very first frame of the animation visible all the time.

As soon as you release the key, anim_player.play() stops resetting the animation back to start, and it can actually proceed to play the following frames.

A simple straight-forward solution would be to remember the last animation you played, and only call play() as soon as it changes."

This is great. The problem is I don’t know how to do this. I’ve been able to figure things out thus far but I’m stumped on how to do this. I know what I want to do, and how to do it, but I don’t know what codes to use. If anybody can help it would be fantastic.

If there is a decent resource to use for GDscripting (as in being taught the script not just the information Godot provides) it would also be very much appreciated. That way I don’t run into any more cases like this.

:bust_in_silhouette: Reply From: avencherus

It’s hard to discern exactly what your question is…

It sounds like you’re not familiar with if statements and flags.

You’ll want to test the animation player to see if it’s playing, before replaying. There is a method on AnimationPlayer that will tell you this: AnimationPlayer — Godot Engine (stable) documentation in English

And the stop method takes an optional boolean to decide whether or not to reset the animation to beginning. It’s true by default, but if you set stop(false) it will not reset.

Thankyou for the resources. I get that I was a little un-clear in my question. Your resources did help somewhat though and I was able to see, and think of other scripts I could write.

Thanks!

kaboom1212 | 2017-01-09 20:10

:bust_in_silhouette: Reply From: bruteforce

Do not restart your “walk” animation, when it is already running!
The AnimationPlayer’s get_current_animation() function returns the name of the current animation.

(...)
if(anim.get_current_animation() != "Right Walk"):
	anim.play("Right Walk")
(...)

Thankyou! This is exactly what I was looking for! I see now that I needed to check if the animation was already running. By making a new var “lr_check” I was also able to program a way of checking which side the character was facing.
So my new code is:

if Input.is_key_pressed(68): #68 = D
set_axis_velocity(Vector2(move_speed,0))
lr_check = true
if(anim.get_current_animation() != “Right Walk”):
anim.play(“Right Walk”)
anim.queue(“Idle Right”)
if lr_check == true and !Input.is_key_pressed(68):
if (anim.get_current_animation() != “Idle Right”):
anim.play(“Idle Right”)

and there you go works like a dream! I can use this for program jumping animations later. Thanks for giving me that starter code. :smiley:

kaboom1212 | 2017-01-09 20:08