display a random image every shake

Im really new to this and have been learning by doing, but Im stuck momentarily…any help is great! :slight_smile:

I want to trigger a function showing a random image everytime I shake.

I have the code written to the point where I can shake and it will display 1 image.

I figure I can create a table and call a random image when a shake is registered, but I cant seem to figure it out, any help?

[import]uid: 3715 topic_id: 428 reply_id: 300428[/import]

use a random number?

everytime you shake, choose a random number between the number of images you have and then display that image

eg:

numberofimages = 20;

when shake - pickrandomnumber between 1and20
display image with the random number

c [import]uid: 24 topic_id: 428 reply_id: 802[/import]

Am I completely off base here? I know Im doing something wrong, please excuse my amateur programming skills, If I need to scrap it and start over, let me know.

local headertext = display.newText(“Shake, Shake, Shake!”, 40, 40, nil, 24)
headertext:setTextColor(255,255,255)

local logotext = display.newText(“SHAKE ME!”, 90, 70, nil, 24)
logotext:setTextColor(255,255,255)

function shakeme( event )
local x = math.random(1,3)
if x == 1 then
local image = display.newImage (“ladybug.jpg”, 0, 0)
elseif x == 2 then
local image = display.newImage (“rocks.png”, 0, 0)
elseif x == 3 then
local image = display.newImage (“rose.jpg” 0, 0)

if (event.isShake) then
local image = display.newImage (x) --I think this where I go wrong.
media.playEventSound( “beep.caf” )

end
end

Runtime:addEventListener(“accelerometer”,shakeme) [import]uid: 3715 topic_id: 428 reply_id: 808[/import]

Tables are your friend :slight_smile:

[code]local img = { “ladybug.jpg”, “rocks.png”, “rose.jpg” };

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

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

I can’t test the code right now, but it should work - well, kind of. It’s not the best way to do it (you should preload images first), but it should give you a starting point.

Quick note : in Lua, # is used to get the length of an array.

Again, I didn’t test this, so I might be wrong (I’m new to Corona and not used to Lua) but that’s how I’d do it.

Tom
PS: Carlos, how do you indent code on this forum ? Tabs get stripped… [import]uid: 4566 topic_id: 428 reply_id: 809[/import]

Ok, this works :

[code]local img = { “a.png”, “b.png”, “c.png” };

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);[/code] [import]uid: 4566 topic_id: 428 reply_id: 811[/import]

I think you have to use local r = math.random(1, #img) because the first index element in Lua is 1 rather than 0, as it is in some other languages. [import]uid: 1560 topic_id: 428 reply_id: 813[/import]

Thanks guys for the help! Works like a champ!

:slight_smile: [import]uid: 3715 topic_id: 428 reply_id: 815[/import]

I wish this post was more recent because I have a very similar issue now.

How would I do something very similar to the OP’s original post here, but with a shake triggering a series of random events, whereby on shake a random background image displays with an accompanying sound (each image has its own corresponding sound), as well as an accompanying message, which is also tied to each specific background? 

So, there would be a list of 20 background images, each with its own message and sound, triggered randomly on someone shaking their device. What’s the best way to go about doing something like this? 

Would it be to target an EventListener to a function, within which there is an if statement with 19 elseif’s? Or would another approach be better?

I’m just bumping this… *Bump* 

Lookin’ for a little help. I’m afraid that I’ll take the wrong course early on in development, and I’ll regret it later. 

Please only bump a post after 24 hours.

Rob

I wish this post was more recent because I have a very similar issue now.

How would I do something very similar to the OP’s original post here, but with a shake triggering a series of random events, whereby on shake a random background image displays with an accompanying sound (each image has its own corresponding sound), as well as an accompanying message, which is also tied to each specific background? 

So, there would be a list of 20 background images, each with its own message and sound, triggered randomly on someone shaking their device. What’s the best way to go about doing something like this? 

Would it be to target an EventListener to a function, within which there is an if statement with 19 elseif’s? Or would another approach be better?

I’m just bumping this… *Bump* 

Lookin’ for a little help. I’m afraid that I’ll take the wrong course early on in development, and I’ll regret it later. 

Please only bump a post after 24 hours.

Rob

Hey guys! Well im just going to update this post and make it more clear…

 local img = { "A.png", "A2.png", "A3.png" } local r = math.random( 1, #img ) local Player = display.newImageRect( img[r], 25, 25) Player.x = display.contentCenterX Player.y = 450 sceneGroup:insert(Player)

So everytime it will pick a different image out of the three…

Good Luck!

  • SonicX278

Hey guys! Well im just going to update this post and make it more clear…

 local img = { "A.png", "A2.png", "A3.png" } local r = math.random( 1, #img ) local Player = display.newImageRect( img[r], 25, 25) Player.x = display.contentCenterX Player.y = 450 sceneGroup:insert(Player)

So everytime it will pick a different image out of the three…

Good Luck!

  • SonicX278