How do you stop AnimatedSprite2D once animation is down?

Tried changing the _ray_gun stuff to it’s $AnimatedSprite2D/RayGun counterpart but it still didn’t work

This is the code for the variable versions of the animated sprite 2ds
@onready var _animated_sprite = $AnimatedSprite2D
@onready var _ray_gun = $AnimatedSprite2D/RayGun

Can you post the entire script, using three tickies like so for formatting?

```
extends Node2D

var my_script_goes_here = 0
```

extends CharacterBody2D

const SPEED = 400.0
const JUMP_VELOCITY = -400.0
const bulletPath = preload(‘res://bullet.tscn’)
const BulletLeftPath = preload(‘res://bullet_left.tscn’)
var walking = false
var rightleft = true
var direction
var firing = false

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _physics_process(delta):
var direction = Input.get_axis(“Up”, “Down”)
if direction:
velocity.y = direction * SPEED
else:
velocity.y = move_toward(velocity.x, 0, SPEED)
direction = Input.get_axis(“Left”, “Right”)
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
shooting()

@onready var _animated_sprite = $AnimatedSprite2D
@onready var _ray_gun = $AnimatedSprite2D/RayGun
@onready var _other_ray_gun = $AnimatedSprite2D/RayGunOtherSide
@onready var _FireAnim = $AnimatedSprite2D/RayGun/RayGunFire
@onready var _FireAnimOtherSide = $AnimatedSprite2D/RayGunOtherSide/RayGunFireOtherSide

func _process(_delta):
if Input.is_action_pressed(“Right”):
walking = true
if Input.is_action_pressed(“Sidle”) != true:
rightleft = true
if Input.is_action_pressed(“Right”) != true and Input.is_action_pressed(“Left”) != true and Input.is_action_pressed(“Up”) != true and Input.is_action_pressed(“Down”) != true:
walking = false
if walking == false and rightleft == true:
_animated_sprite.play(“IdleRight”)
if Input.is_action_pressed(“Left”):
walking = true
if Input.is_action_pressed(“Sidle”) != true:
rightleft = false
if walking == false and rightleft == false:
_animated_sprite.play(“IdleLeft”)
if Input.is_action_pressed(“Up”):
walking = true
if Input.is_action_pressed(“Down”):
walking = true
if walking == true and rightleft == true:
_animated_sprite.play(“WalkRight”)
if walking == true and rightleft == false:
_animated_sprite.play(“WalkLeft”)
if rightleft == true and firing == false:
_ray_gun.play(“IdleRight”)
_other_ray_gun.play(“InvisibleAnim”)
if rightleft == false and firing == false:
_ray_gun.play(“InvisibleAnim”)
_other_ray_gun.play(“IdleLeft”)
move_and_slide()

func shooting():
if Input.is_action_just_pressed(“Shoot”) and rightleft == true and firing == false:
firing = true
_ray_gun.play(“BANGRight”)
_other_ray_gun.play(“InvisibleAnim”)
var bullet = bulletPath.instantiate()
get_parent().add_child(bullet)
_FireAnim.play(“Fire”)
bullet.position = $AnimatedSprite2D/Marker2DRight.global_position
if Input.is_action_just_pressed(“Shoot”) and rightleft == false and firing == false:
firing = true
_ray_gun.play(“InvisibleAnim”)
_other_ray_gun.play(“BANGLeft”)
_ray_gun.play(“InvisibleAnim”)
var BulletLeft = BulletLeftPath.instantiate()
get_parent().add_child(BulletLeft)
_FireAnimOtherSide.play(“FireOther”)
BulletLeft.position = $AnimatedSprite2D/Marker2DLeft.global_position

func _ready() → void:
_ray_gun.animation_finished.connect(_on_ray_gun_animation_finished)

func _on_ray_gun_animation_finished() → void:
if _ray_gun.animation == “BANGRight”:
_ray_gun.play(“IdleRight”)
firing == false

Please use the ticks next time or edit your current post, it will help format code to be legible.

What doesn’t work specifically? What do you expect to happen and what really happens?

It could be that it only plays for “BANGRight” in which case you need to add another similar connection for _other_ray_gun. There might be an indentation error that I cannot see because of the formatting.

the move_and_slide() function should be part of _physics_process instead of just _process, though I do not believe this is related to the animations.

What I want to happen is I want the animation to return to idle after the BANGRight animation, and to return to the firing == false state that is usually how the player character is. Also, where do you want me to put the ticks? I have spaces in between the lines

```
paste all code here

spaces between lines don’t matter

     spaces as indentation are shown
```

For the firing state I suspect this line is giving you a warning, something along the lines of “statement has no effect”

func _on_ray_gun_animation_finished() -> void:
    if _ray_gun.animation == "BANGRight":
        _ray_gun.play("IdleRight")
        firing == false # << warning here

it should be firing = false with one equals sign for assignment, two is for comparison; so the line was comparing if firing was false and doing nothing with it.

Does the idle animation not play? does any other animation play? what really happens?

1 Like

Thanks, it worked! Somehow I missed that double = sign, usually I catch those, or the game warns me about them.

1 Like

Ah good! in the project settings you can change specific warning into errors or all warnings to errors, highly recommend it for some tricky lines like this.

1 Like