- Edited
Just hours ago I started trying to learn Godot and am following the my first 2d game tutorial, but I wanted to add one extra button that would make the player invincible just so I can understand actions other than moving. I've spent hours on this and figured out that the if statement used to start the "invincibility" animation just refreshes every frame but I still can't find a way to make that animation play and override the movement animations. Thank you in advance, it is appreciated.
For reference, the animation works when I replace my walking animation with it. Also, when I try using a While statement, it just crashes the second I press the space bar (invincibility button).
extends Area2D
@export var speed = 400
var screen_size
var is_invincible = false
func _ready():
screen_size = get_viewport_rect().size
func _process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("move_right"):
velocity.x += 1
if Input.is_action_pressed("move_left"):
velocity.x -= 1
if Input.is_action_pressed("move_down"):
velocity.y += 1
if Input.is_action_pressed("move_up"):
velocity.y -= 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed
$AnimatedSprite2D.play()
else:
$AnimatedSprite2D.stop()
position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)
if velocity.x != 0:
$AnimatedSprite2D.animation = "walk"
$AnimatedSprite2D.flip_v = false
# See the note below about boolean assignment.
$AnimatedSprite2D.flip_h = velocity.x < 0
elif velocity.y != 0:
$AnimatedSprite2D.animation = "walk"
if velocity.x < 0:
$AnimatedSprite2D.flip_h = true
else:
$AnimatedSprite2D.flip_h = false
if Input.is_action_pressed("invincible"):
is_invincible = true
$AnimatedSprite2D.animation = "invincible"
if Input.is_action_just_released("invincible"):
is_invincible = false
if is_invincible != true:
$AnimatedSprite2D.play("invincible")
is_invincible = true