setVisible() having trouble

I wrote a function two flip back and forth between 2 sprites (see below), I’m using setVisible to [show sprite 1 - hide sprite 2] then [hide sprite 1 show sprite 2] but it’s not working, I’m thinking it actually has something to do with updating the sprites afterward but I can’t figure it out.

It starts out right (with sprite 2 hidden and sprite 1 showing) then after second sprite 2 shows up but sprite 1 doesn’t hide.

function AnimateHelo()
local helo_01 = playdate.graphics.sprite.new()
local helo_01_img = playdate.graphics.image.new(“images/helo_01.png”)
helo_01:setImage(helo_01_img)
helo_01:setCenter( 0.0, 0.0 )
helo_01:moveTo(281, 27)
helo_01:add()

local helo_02 = playdate.graphics.sprite.new()
local helo_02_img = playdate.graphics.image.new("images/helo_02.png")
helo_02:setImage(helo_02_img)
helo_02:setCenter( 0.0, 0.0 )
helo_02:moveTo(281, 27)
helo_02:add()


delay -= 1
if delay < 0 then 
	delay = 30
	helo_frame +=1
	if helo_frame > 2 then
		helo_frame = 1
	end	
end

if helo_frame == 1 then
	helo_01:setVisible(true)
	helo_02:setVisible(false)	
elseif helo_frame == 2 then
	helo_01:setVisible(false)
	helo_02:setVisible(true)
end	

end

Is this all of your code?

You need the playdate.update() function and put the playdate.graphics.sprite.update() call in there

Source.zip (17.3 KB)

That’s just my function, here’s the whole thing, the call is in there.

Brian

OK, you’re defining the sprites in your AnimateHelo() function, so with each call you’re adding more and more sprites each time - and with the current flow they all end up being visible.

So, move the 12 lines that define your two sprites to myGameSetUp() (and helo_01 and helo_02 will no longer be able to be local) and things will run as you intended!

I tested this changed code: TEST.zip (18.1 KB)