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 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” )
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
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]
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]
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 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?