This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

important: I'am using AnimatedSprite
I made animation via documentation godot link: https://docs.godotengine.org/en/stable/tutorials/2d/2d_sprite_animation.html
but in with documentation do only animation run if you input key right and stop animation but i wanna if you not input any key: animations idle
my code:

extends KinematicBody2D

onready var _animated_sprite = $AnimatedSprite

func _process(_delta):
    if Input.is_action_pressed("ui_down"):
        _animated_sprite.play("run")
    elif Input.is_action_pressed("ui_up"):
        _animated_sprite.play("run")
    elif Input.is_action_pressed("ui_left"):
        _animated_sprite.play("run")
    elif Input.is_action_pressed("ui_right"):
        _animated_sprite.play("run")
    else:
        _animated_sprite.stop() 

(I'm add keys up, down and left)
thanks for help <3!

Godot version 3.5.1
in Engine by (21 points)
edited by

Edited to fix code formatting...

1 Answer

0 votes

That else block will execute if none of the 4 input keys are being pressed, so you could replace this:

_animated_sprite.stop()

With something like:

if !_animated_sprite.playing(): 
    _animated_sprite.play("idle")
by (22,704 points)

what if i am using animation player, every animation work properly but not the attack, it only show millisecond of the animation and then return to idle animation
here is the code:

extends KinematicBody2D

var velocity : Vector2

export var max_speed : int = 600
export var gravity : float = 35
export var jump_force : int = 1600
export var acceleration : int = 50
export var jump_buffer_time : int  = 15
export var cayote_time : int = 15

var jump_counter : int = 0
var jump_buffer_counter : int = 0
var cayote_counter : int = 0

func _physics_process(_delta):

if is_on_floor():
    cayote_counter = cayote_time
    jump_counter = 0

if not is_on_floor():
    if cayote_counter > 0:
        cayote_counter -= 1

if jump_buffer_counter > 0 and jump_counter < 1:
    jump_counter += 1
    cayote_counter = 1

velocity.y += gravity
if velocity.y > 2000:
    velocity.y = 2000

if Input.is_action_pressed("right"):
    velocity.x += acceleration 
    $Sprite.flip_h = false
    get_node("AnimationPlayer").play("run")
elif Input.is_action_pressed("left"):
    velocity.x -= acceleration
    $Sprite.flip_h = true
    get_node("AnimationPlayer").play("run")
elif Input.is_action_pressed("jump"):
    get_node("AnimationPlayer").play("jump")
elif Input.is_action_just_pressed("attack"):
    attack()
else:
    velocity.x = lerp(velocity.x,0,0.2)
    get_node("AnimationPlayer").play("idle")

velocity.x = clamp(velocity.x, -max_speed, max_speed)

if Input.is_action_just_pressed("jump"):
    jump_buffer_counter = jump_buffer_time
    get_node("AnimationPlayer").play("jump")

if jump_buffer_counter > 0:
    jump_buffer_counter -= 1

if jump_buffer_counter > 0 and cayote_counter > 0:
    velocity.y = -jump_force
    jump_buffer_counter = 0
    cayote_counter = 0

if Input.is_action_just_released("jump"):
    if velocity.y < 0:
        velocity.y += 400

velocity = move_and_slide(velocity, Vector2.UP)


func attack_detected():
print("hit")

func attack():
get_node("AnimationPlayer").play("Attack")
print("attack")


func _on_AttackBox_body_entered(body):
if body.has_method('attacked'):
    body.attacked()
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 Frequently asked questions and 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 [email protected] with your username.