full screen sprite animation

I’m working on an app which I want to start off with a full screen animation. I have created a sequence of 30 sprites at 1024 x 768 pixels and imported them into Codea with Dropbox.

I then display these sprites, one each frame, using a counter like this :

sprite(“Dropbox:spritename”…counter, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)

I noticed that Codea does not manage to do this at 60 fps. But when I added a restart function taking the program back to this start animation, I noticed that it DOES run at this speed (or nearly) when it is performed a second time.

So I guess it’s caching the sprites which is slowing it down the first time ?

I have added a “prefetch” routine so that now all the sprites are first displayed at size 0 x 0, and then again at full size. This works, but it also seems to cause Codea to crash more often.

Perhaps this is not necessary because this problem does not happen once it is exported to xCode ? Or does that have some kind of prefetch that I have to add there ? Or is there a good solution in Codea for this ?

You are going to want to toss all your frames into a table in the setup function before drawing them. Try something like this:

function setup()
    frames = {}
    for i=1,30 do
        frames[i] = readImage("Dropbox:spritename"..tostring(i))

Then draw the frames with sprite(frames[currentFrame], WIDTH/2, HEIGHT/2, WIDTH, HEIGHT). This will prevent Codea from reading from your Dropbox every frame.

Can you test to make an array with the list of images in setup like this :

array = {}
for i=1, n do
     array[#array+1] = readImage("Dropbox:spritename" .. i)


And after, in the draw function, use
     sprite(array[counter], WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)


I tell you to test this, because i'm not sure of result...

[EDIT] : Lol... Slashin8r have already respond...

Hehe, beat ya to it, muahahahaha :stuck_out_tongue:

thanks guys, i’ll try this and let you know.

In the meantime I figured out that the Codea crashes occur when I call all the big sprites I’m using. Apart from the intro animation I also have some info pages, separate ones for portrait and landscape layout. For high quality and to be able to use alpha transparencies they are all png’s and also they are 2048 x 1536 for retina screen. Could it be that this is causing the cache to overflow ?

When I go through my info pages In landscape it still works, but when I rotate and then go through the portrait pages (or the other way around) Codea crashes after a few pages in the second layout…

Interestingly the animations work fine with the “prefetch” I do now, but then when I exit the program and then start it again, Codea crashes every time…

Is there some limit to the amount and file size of the sprites ?

This might help explain the memory limits on the iPad


@eriksw, are all your images being stored into either a table or separate variables first? Too many sprite calls to the direct image causes Codea to try and read that image too many times. Store the images using readImage in a place other than the draw function, like setup, and then draw them. Example below:

function setup()
    image = readImage("Documents:Galaxy")

function draw() 
    background(40, 40, 50)
    -- correct way to draw images as it uses the image stored in the variable image
    sprite(image, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    -- incorrect way to draw images as it forces Codea to read the image every frame
--  sprite("Documents:Galaxy", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)

ok get it. it will take a bit of work to do it like this, because i’m using an awful lot of sprites in my app… so that’s why it was getting more and more unstable as i was adding stuff like info pages (even more sprites).

i’ll definitely let you know the results when i’m done changing this.

thanks for the help !