Flipping an Animation (solved)

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

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")

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. :slight_smile: Have fun coding everyone!

matbog23 | 2021-02-13 20:18

:bust_in_silhouette: Reply From: literalcitrus

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")

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?

KND2501 | 2018-02-15 05:11

:bust_in_silhouette: Reply From: KND2501

This is what i was looking for.

$Sprite.flip_h = true
:bust_in_silhouette: Reply From: Chevon

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.

I recommend this answer, it is quick and easy.

example
$YourNode2D.scale.x = -1

That’s it, all flipped.

bath_idea | 2022-11-27 05:19