0 votes

Hi, I am currently having trouble where I am trying to play the attack animation like any normal game where you just press the button and it plays the animation. At first, the animation wasn't playing at all, I fixed that, but then ran into another problem where it would play the animation continously while the character is moving either left or right ( I couldn't change directions).

Now I got it to move the character left and right, but the animation is still continuously playing.

Any Help?

Video Display of Problem

extends KinematicBody2D

# class member variables go here, for example:
# var a = 2
# var b = "textvar"

const SPEED = 60
const GRAVITY = 10
const JUMP_POWER = -260 # ' - ' in the y-axis means up
const FLOOR = Vector2(0,-1)

var velocity = Vector2()
var on_ground = false
var attack = false
var animation = ""

#to use an object use $NameofObject

func _input(event):
    #attack = false
    if Input.is_key_pressed(KEY_X) and is_on_floor():
        attack = true
    pass

func _physics_process(delta):
    print("attack: ",attack)
    if Input.is_action_pressed("ui_right"):
        velocity.x = SPEED
        animation = "run"
        $AnimatedSprite.flip_h = false
    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
        animation = "run"
        $AnimatedSprite.flip_h = true
    else:
        velocity.x = 0
        if on_ground == true:
            animation = "idle"


    if Input.is_action_pressed("ui_up"):
        if on_ground == true:
            velocity.y = JUMP_POWER
            on_ground = false

    velocity.y += GRAVITY

    if is_on_floor(): #not in the air
        on_ground = true
    else: # in the air
        on_ground = false
        #$AnimatedSprite.play("fall")
        if velocity.y < 0:
            animation = "jump"
        elif (velocity.y >= 0) and velocity.y != 20:
            animation = "fall"

    if attack:
        animation = ("ground-attack1")

    $AnimatedSprite.play(animation)
    #attack = false


    velocity = move_and_slide(velocity, FLOOR)
in Engine by (12 points)

1 Answer

0 votes

Hey I have the same problem almost, but mine is worst. Instead my sprite freezes whenever I press the key that is supposed to make the sprite attack.

if Input.iskeypressed(KEY_E):
print("Attack is working")
$Sprite.play("Attack")

this is my code. Is there anything wrong with it. Sorry if you expected an answer, I am like, a really big noob.

by (16 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.