Animated Sprite changing frame

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mrpaolosarino

I want to create a breakable block. I create an animated sprite in which it has 4 frames that represent the block state. How do I set the frame of the animated sprite manually?
it seems like $AnimatedSprite.frame = 0 is not working.

EDIT: Here’s my script:

extends StaticBody2D
var Life = 100
func _physics_process(delta):
         match Life:
              100:
                 $AnimatedSprite.frame = 0
               75:
                 $AnimatedSprite.frame = 1
               50:
                 $AnimatedSprite.frame = 2
               25:
                 $AnimatedSprite.frame = 3
               0:
                 $AnimatedSprite.frame = 4
                 queue_free()

As I debug it, the match statement stays in the first expression even if the life is not 100.
I guess that this is not how the match function is used, either way, can anyone know how to implement this?

EDIT: I food the problem. The problem is that the match function needs to be accurate literally. But my damage is 6 so it makes sense that it doesn’t hit those match statement.

What I need now is to add “>” in the match function but I don’t know how

EDIT: F… this match function, what I do is normal if’s and it solved the problem

if Life >= 100:
	$AnimatedSprite.frame = 0
elif Life >= 75:
	$AnimatedSprite.frame = 1
elif Life >= 50:
	$AnimatedSprite.frame = 2
elif Life >= 25:
	$AnimatedSprite.frame = 3
elif Life >=0:
	$AnimatedSprite.frame = 4
	queue_free()
$AnimatedSprite.frame = int(4 - floor(Life / 25.0))
if Life <= 0: queue_free()

This should also work and looks a bit nicer. And should also run faster.
Just my opinion.

whiteshampoo | 2021-04-02 06:32