+1 vote

Hello, need a little help. I'm asking two things here.

  1. How do i flip the sprites/animations? I don't seem to get set_flip_h(true) working when i refer to the AnimationPlayer.

  2. How do i make my animations work in the game? I have a run cycle only showing a single frame, the first frame.

    if Input.is_action_pressed("ui_right"):
    motion.x = WALK
    sprite_node.play("RUN")
    
in Engine by (103 points)
edited by

You can always simply get a sprite with a different run for right and left and then bind those animations to the keybind. But if you really want to work like this then the option that worked for me is:

if Input.is_action_pressed("right"):
    motion.x += ACCELERATION
    $Sprite.flip_h = false
    $AnimationPlayer.play("Run")

elif Input.is_action_pressed("left"):
    motion.x -= ACCELERATION
    $Sprite.flip_h = true
    $AnimationPlayer.play("Run")

With the ACCELERATION being my build up speed variable, Sprite the name of my sprite node, and AnimationPlayer the name of my AnimationPlayer node. I looked around but never noticed this option anywhere so I thought that I had to comment on it. :) Have fun coding everyone!

3 Answers

0 votes

1/ AnimationPlayer doesn't have a flip_h property. You'll need to set that on the Sprite itself using either flip_h or set_flip_h().

2/ When you use play(anim) it starts the animation from the beginning. In your code you're repeatedly calling play() on the AnimationPlayer every frame that the right key is being held down, do it's constantly restarting the animation. You want to play the animation only when the key is first pressed, and stop it (or play a different animation) when you release it. An easy way to do this is to use Input.is_action_just_pressed(action) which will only be true for the first frame of the key being pressed. eg:

 if Input.is_action_just_pressed("ui_right"):
    sprite_node.play("RUN")
by (514 points)

I've been sick so i have been occupied with my bed for a bit.

Anyway i actually got the h-flip to work on my KinematicBody2D.

    if Input.is_action_pressed("ui_right"):
    motion.x = WALK
    $"P1 Sprites/AnimationPlayer".play("RUN")
    $"P1 Sprites".flip_h = false

Now my character moves and faces the direction its moving to. But yeah, still no animation since its just showing the first frame continously. So if i am going to use...

if Input.is_action_just_pressed("ui_right"): 
$"P1 Sprites/AnimationPlayer".play("RUN")

... where am i going to put these lines?

–1 vote

This is what i was looking for.

$Sprite.flip_h = true
by (103 points)
+1 vote

A bit late butyou can in a sense rotate the AnimationPlayer by making it a child of a node the flip the node using its scale property on the x axis scale.x = -1 or 1 respectively.

by (29 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.