Shake loads random images - how to create intro image

I found some code on here to randomly load images I have created when user shakes the device (I forgot where I got it so if you recognize it THANK YOU!). Each time the program loads in the simulator it starts with a black screen, then you have to shake to start seeing the images. I want to have an intro image always load on start up and then never load again once the shaking and randomness start.

How can I do this?
Here is the code I’m using (not sure how you make the code look like it does in text Wrangler):
local img = { “cleanup.gif”, “distortion.gif”, “heart.gif”, “opposite.gif”, “randomize.gif”, “showoff.gif”, “stupid.gif” };

local function onShake(event)
if event.isShake then
local r = math.random(0, #img);
local image = display.newImage(img[r], 0, 0);
end

return true;
end

Runtime:addEventListener(“accelerometer”, onShake);

And to make it even cooler are there some effects or transitions I can apply to each image load?

Thanks

[import]uid: 28819 topic_id: 5661 reply_id: 305661[/import]

Billy,

You can declare a background first inside your main.lua:

[lua]image = display.newImage( “background.jpg” )[/lua]

Note that this is not a local variable as you need a reference to it in order to call a transition.

And then inside your shake event you can fade it from the screen call a function to load the next image in.

[lua]transition.to( image {time=100, alpha=0.0, onComplete=function() loadImage( img[r]) end } ) – Load[/lua]

To make the new image fade in you need another function:

[lua]function loadImage( filename )
image = nil
image = display.newImage( filename )
image.alpha = 0 – Make it invisible so we can fade from it
transition.to( image {time=100, alpha=1.0 } ) – Transition next image in
end[/lua]

Code is untested (hope it doesn’t have errors) but it should point you in the right direction.

[import]uid: 11393 topic_id: 5661 reply_id: 19414[/import]

Thanks for this! Intro page now works (background)

However this part:

transition.to( image {time=100, alpha=0.0, onComplete=function() loadImage( img[r]) end } ) – Load

Not sure what it is supposed to do. I put it in there but see no change. Am I putting it in the wrong place? Do I need change a variable?

Also, how are you pasting code into this forum and having it format like in Text Wrangler? [import]uid: 28819 topic_id: 5661 reply_id: 19573[/import]

The transition.to function is supposed to go inside your onShake handler which is triggered by the shake event listener.

It’s supposed to fade your new image in when the phone shakes.

In your forum post, place your code in between < lua> and < /lua> tags (remove the leading space) to get code formatting. [import]uid: 11393 topic_id: 5661 reply_id: 19574[/import]

your random number should start with 1. Lua tables do not have a 0 index. [import]uid: 6645 topic_id: 5661 reply_id: 19794[/import]

@Jmp: you’re right. Lua tables start with index 1, and img has valid indexes of 1…n. Good catch.

Lua does allow for a “hidden” index at table[0] but it has to be set explicitly. [import]uid: 11393 topic_id: 5661 reply_id: 19801[/import]

Thanks, making a little headway but still not sure exactly where to put the new code.
I know this going to look dumb to some of you, but based on what I’ve got here the best I can figure is this so far:

[lua]–> hide status bar usingsetStatusBar()
display.setStatusBar( display.HiddenStatusBar )

image = display.newImage( “intro.png” )

local img = { “cleanup.gif”, “distortion.gif”, “heart.gif”, “opposite.gif”, “randomize.gif”, “showoff.gif”, “stupid.gif” };

local function onShake(event)
if event.isShake then
local r = math.random(1, #img);
local image = display.newImage(img[r], 0, 0);

media.playEventSound( “beep.wav” )

transition.to( image {time=1000, alpha=0.0, onComplete=function() loadImage( img[r]) end } ) – Load

function loadImage( filename )
image = nil
image = display.newImage( filename )
image.alpha = 0 – Make it invisible so we can fade from it
transition.to( image {time=1000, alpha=1.0 } ) – Transition next image in

end

end
return true;
end

Runtime:addEventListener(“accelerometer”, onShake);[/lua]

So this affects behavior of the program in no way. Obviously I need to put in something where it says filename as in:

function loadImage( filename )

and

image = display.newImage( filename )

but since I want it to be random I don’t want to put actual file names in here right?

Tried a bunch of things but either it makes no difference or doesn’t work at all.

Sorry if this is a mess. Working my way thru the tutorials but still struggling with basic layout/rules concepts. [import]uid: 28819 topic_id: 5661 reply_id: 19891[/import]