Godot Version
Godot-4
Question
So, when I press the left key of the mouse to start the attack, it show only one frame and stop immediately, can someone tell me why?
Code:
extends CharacterBody2D
const SPEED = 150.0
const JUMP_VELOCITY = -270.0
@onready var sprite_2d = $Sprite2D
@onready var animation_player = $AnimationPlayer
var Attacking = false
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _on_Movement_animation_finished():
Attacking = false
func _physics_process(delta):
#animation
if (velocity.x > 1 || velocity.x < -1):
sprite_2d.animation = "Running"
else:
sprite_2d.animation = "default"
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
sprite_2d.animation = "Fall"
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
sprite_2d.animation = "Jumping"
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
sprite_2d.flip_h = velocity.x < 0
else:
velocity.x = move_toward(velocity.x, 0, 30)
if Input.is_action_just_pressed("attack"):
sprite_2d.play("attack")
move_and_slide()