0 votes

For two days I have been trying to get the attack animation for my player sprite to play completely, but it will only play the first frame. I have moved things around in my code quite a bit, but to avail. I've looked around at possible reasons for this to keep happening and tried to fix those things, but it still won't play completely.
This is my current code.

`func physicsprocess(delta):

motion.y += GRAVITY

if Input.is_action_pressed("ui_right"):
    if attacking == false:
        motion.x = SPEED
        $AnimatedSprite.flip_h = false
        $AnimatedSprite.play("Run")
elif Input.is_action_pressed("ui_left"):
    if attacking == false:
        motion.x = -SPEED
        $AnimatedSprite.flip_h = true
        $AnimatedSprite.play("Run")
else: 
    if attacking == false:
        motion.x = 0
        $AnimatedSprite.play("Idle")

if is_on_floor() && attacking == false:
    if Input.is_action_pressed("ui_up"):
        motion.y = JUMP_HEIGHT
    if Input.is_action_just_pressed("ui_down") && attacking == false:
        attacking == true
        $AnimatedSprite.play("Attack")
        if motion.x == 0:
            if $AnimatedSprite.flip_h == false:
                motion.x = 15
            elif $AnimatedSprite.flip_h == true:
                motion.x = -15
        else: 
            if $AnimatedSprite.flip_h == false:
                motion.x = SPEED + 15
            elif $AnimatedSprite.flip_h == true:
                motion.x = -SPEED + -15
else:
    if motion.y < 0:
        $AnimatedSprite.play("Jump")
    else: 
        $AnimatedSprite.play("Fall")


motion = move_and_slide(motion, UP)`

Am I doing something wrong? Is there something I need to change or add?

asked Jun 9 in Projects by RodFire8181 (12 points)

Backticks ` only work for one-liner. Use the { } button to format a block of code.

Thank you. Good to know.

1 Answer

0 votes

Found a typo here:

attacking == true
$AnimatedSprite.play("Attack")

Should be:

attacking = true
$AnimatedSprite.play("Attack")

Coding Tips:

Instead of writing

if attacking == false:
# or
if $AnimatedSprite.flip_h == true:
# and 
if $AnimatedSprite.flip_h == false:

write

if not attacking:
# or
if $AnimatedSprite.flip_h:
# and
if not $AnimatedSprite.flip_h:
answered Jun 9 by Dlean Jeans (3,505 points)
edited Jun 9 by Dlean Jeans

Thank you. This didn't fix my proble but I appreciate it.

Try creating a function called is_attacking and use it in place of your attacking variable:

func is_attacking():
  return is_playing() and animation == "Attack"
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.