How to add a sprite frame on each click?

Godot Version

4.6.2

Question

Hello!!

So I’m making a sort of tiny ‘escape room’ 2d minigame, and while I’ve made a sprite clickable, I need to change it to be +1 frame every single time I click it, since it’s a 1-9 number lock, functionally.

I’ve tried about 5-6 different ways to make this happen, and while I kinda understand why the code I have doesn’t work, I can’t seem to understand what I do need to put in. For context, here’s the last try I had (although I don’t have the other five to show. I did look through the documentation already to try and find something to help, and I found what I have now from it, although if someone could point out if I missed something that will fix this, please do!! I’m very new to reading documentation lol)

var clickable: bool = false
@onready var animated_sprite = $"."
@onready var current_frame = animated_sprite.get_frame()

func _process(delta: float) -> void:
	slot_changable()

func slot_changable():
	if clickable:
		if Input.is_action_just_pressed("clicked"):
			print("mouseclicked")
			current_frame +1

clickable is getting called. But when I ran this, it gave me absolutely no errors. What did I do wrong? Would I need to make a separate animation for each number switch? I just thought this would be easier, less ‘the same thing every time’ if I could just put in a +1 sprite frame each time i clicked. Is that not possible or am i just doing it very wrong lol?

Your variable here:

@onreadyonreadyonreadyonreadyonreadyonreadyonreadyonready var current_frame = animated_sprite.get_frame()

You just get the frame number which is an int representing the index number of the current frame.

So when you += 1 you add 1 to your 5, you don’t progress one frame in the animation.

I think instead you could could define your variable as below for it to work:

animated_sprite.frame += 1

(where now you are updating current_frame)

This does nothing because the value isn’t assigned to anything. You need to do one of the following:

current_frame = current_frame + 1
#or the short version
current_frame += 1

I use this to animate an airplane propeller. There are four sprites on the sheet and I cycle through them.

func _advance_prop() -> Signal: 
	sprite.frame = prop_count
	prop_count += 1
	if prop_count > 3: prop_count = 0
	await get_tree().process_frame
	return process_done

dragonforge is correct, to build on this you should get a warning along the lines of “this statement has no effect”, because Godot does the math of “current_frame + 1” and throws the answer away, rather than storing it back into current_frame. Make sure to check your script warnings!

#Assuming 9 frames, because the first frame is zero
var last_frame: int = 8

current_frame = wrapi(current_frame + 1, 0, last_frame)

This would actually be a better way of doing it since you know the number of frames.

thank you it worked!! One more quiestion, how would I get it to go back to zero after clicking again on the last frame? Becuase i tried this:

if animated_sprite.frame = 10:

forgetting I couldnt assign a number in an ‘if’ thing, so it didnt work.

If you want to check for comparison you do ==

Using just one = means you want to change whatever is before the = to whatever is after it.

So if it == is or >= 10, reset to the frame you want it to restart from

thank you sm!!!

No worries! Have a nice day

You could also do this:

animated_sprite.frame = wrapi(animated_sprite.frame + 1, 0, 9)

It’ll wrap around for you after frame 9.