Question: Howto change a sprite after a certain amount of time

Hi all, I’m new to Codea and to Lua, but I’ve been fiddling with coding in for example gamemaker (GML) enough to enjoy the puzzles to get something done. I’ve been catching up with the tutorials of David (Reefwing) to get a jumpstart and I ran into some basic approach questions with regards to drawing sprites.

I’ve tried the Button class from this topic, but I’m sure my question applies for all drawn sprites
http://twolivesleft.com/Codea/Talk/discussion/1298/button-class-with-sprites#Item_16

How would one go about if you wanted to keep the sprite “chest open” for a certain amount of time/frames before it changes back to the closed chest? Or, how would you have a certain sprite shown for a set time and after that timeframe change remove that sprite to draw something else?

Kind regards,
Robin

Not that this is any better but here is another way to do it. This gives you the option of more images based on different variables. The state variable becomes a kind of state machine (I think that’s the right term) so you could have multiple states and a different picture for each one.

Just did a quick edit on this since I missed changing one of the variables name in one line. I went ahead and added a third image. Now if you touch the screen while the chest is open you get a gem. It still resets after 5 seconds to the closed chest.
Let me know what you think.



function setup()
    chestImg = {}
    chestImg[1] = {img = readImage("Planet Cute:Chest Closed")}
    chestImg[2] = {img = readImage("Planet Cute:Chest Open")}
    chestImg[3] = {img = readImage("Planet Cute:Gem Orange")}
    state = 1
    catchGem = false
    et = 0
end

function draw()
    background(40,40,50)
    fill(255)
    text("tap screen to open chest for 5 seconds",WIDTH/2,HEIGHT/2+200)
    chest()
end

function chest()
    if ElapsedTime-et>5 then
        state = 1
    end
    sprite(chestImg[state].img,WIDTH/2,HEIGHT/2)

end

function touched(t)
    if t.state==BEGAN then
        state = 2    -- open the chest
        et=ElapsedTime    -- set time
    end
    if state == 2 and t.state == ENDED then
        catchGem = true
    end
    if t.state == BEGAN and state == 2 and catchGem == true then
        state = 3
        catchGem = false
    end
end

@Robto Here’s a simple example.


function setup()
    chestClosed=readImage("Planet Cute:Chest Closed")
    chestOpen=readImage("Planet Cute:Chest Open")
end

function draw()
    background(40,40,50)
    fill(255)
    text("tap screen to open chest for 5 seconds",WIDTH/2,HEIGHT/2+200)
    chest()
end

function chest()
    if open then
        sprite(chestOpen,WIDTH/2,HEIGHT/2)
        if ElapsedTime-et>5 then
            open=false
        end
    else
        sprite(chestClosed,WIDTH/2,HEIGHT/2)
    end
end

function touched(t)
    if t.state==BEGAN then
        open=true    -- open the chest
        et=ElapsedTime    -- set time
    end
end

@Robto, I would do something like img = readImage('Planet Cute:Chest Closed') and then a sprite(img, x, y) then in the timer callback you would simply change img to the new image you want

Thank you for your reply. I’ll look it up!

Kind regards,
Robin

I kept browsing and found, I think, the answer in the astroids tutorial. Introduce a timer and when that timer reaches x, remove the sprite and draw a new sprite. Is that the correct approach?

You need a timer. ElapsedTime gives you the seconds since the program started, so you could use that (ie store the value when the time starts, and wait for it to exceed that time + X seconds). You would test for that in draw().

Alternatively, DeltaTime gives you the time since the last draw (generally about 1/60 of a second). You could set a counter=0, and increment by DeltaTime each time draw() runs, and watch for it to exceed X seconds.

There is no “removing” a sprite in Codea. You simply draw something else next time. If you haven’t done so already, have a look at my ebook on Codea, below. It explains how drawing works in Codea.

https://www.dropbox.com/s/t5im6tl14ky5t08/Codea%20for%20beginners.pdf