I don't want the attack animation to loop what do I have to change
Asked Answered
B

3

0

extends CharacterBody2D

@export var SPEED : int = 150
@export var JUMP_FORCE : int = 300
@export var GRAVITY : int = 900
const max_jumps = 2
@export var current_jumps = 1
@export var is_Attacking = false

func _physics_process(delta):
run()

Rotate

omdraaien()

Gravity

zwaartekracht(delta)

Jump

jump()

attack

attack()

move_and_slide()

func run():
var direction = Input.get_axis("ui_left","ui_right")
if direction:
velocity.x = SPEED * direction
if is_on_floor() and is_Attacking == false:
$AnimatedSprite2D.play("run")
else:
velocity.x = 0
if is_on_floor() and is_Attacking == false:
$AnimatedSprite2D.play("idle")

func omdraaien():
var direction = Input.get_axis("ui_left","ui_right")
if direction == 1:
$AnimatedSprite2D.flip_h = false
elif direction == -1:
$AnimatedSprite2D.flip_h = true

func zwaartekracht(delta):
if not is_on_floor():
velocity.y += GRAVITY * delta
if velocity.y > 0 and is_Attacking == false:
$AnimatedSprite2D.play("fall")

func jump():
if Input.is_action_just_pressed("Jump"):
if current_jumps < max_jumps and is_Attacking == false:
velocity.y -= JUMP_FORCE
$AnimatedSprite2D.play("jump")
current_jumps += 1
$AnimatedSprite2D.play("jump_double")
if is_on_floor():
current_jumps = 1

func attack():
if Input.is_action_just_pressed("Attack"):
$AnimatedSprite2D.play("attack")
is_Attacking = true

func _on_animated_sprite_2d_animation_finished():
if $AnimatedSprite2D.play == "attack":
is_Attacking = false

Baudekin answered 15/3 at 13:7 Comment(0)
S
0

Mmh... Try wrapping it in code to make it easier to read.
Your problem could be that is_attacking is set to true when the button is pressed, but continues to run the animation.
Use a variable to check if attack animation has been triggered and set it to true, then when button is released, set it to false again (or viceversa).

var attack_finish : bool = false

Attack()

if Input.is_action_just_pressed("Attack"):
     if not attack_finish:
          $AnimatedSprite2D.play("attack")
          is_attacking = true
elif Input.is_action_just_released("Attack"):
     attack_finish = false

_on_animated_sprite_2d_animation_finished

func _on_animated_sprite_2d_animation_finished():
     if $AnimatedSprite2D.play == "attack":
          attack_finished = true
          is_Attacking = false

You might also want to use an AnimationTree. It will make it easier to code transitions and states.

Shepherd answered 15/3 at 13:32 Comment(0)
B
0

Remember to set "loop" parameter to false for your attack animation, if you use AnimatedSprites object.

Bruise answered 18/3 at 5:38 Comment(0)
G
0

Hi there, I changed the tags as this seems more like a help question than a project.

Goldbrick answered 18/3 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.