Problem With Random Code!

Hi

I am completely new to Corona (5 days) but having followed various tutorials, advice etc I have managed to get the first part of my app working using Director Class by Ricardo Rauber. This part is a simple change of scenes (images) using buttons. Thanks to all who helped.

However, (I am sure you thought there was going to be a however!) the next bit (selecting random images) is causing me problems and I have two questions.

1 Should this part be coded using Director Class (selecting random .lua files) OR not?

2 If not, how should it work?

I copied the code below from the forum and the person who posted it says it works, having pasted it into my main lua file and made the appropriate changes to the images. I can’t get this to work.

local img = { “bluebackground.png”, “redbackground.png”, “yellowbackground.png” };

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

return true;
end

Runtime:addEventListener(“accelerometer”, onShake);

I need to be able to select random images on shake, if anyone can provide a definitive answer/piece of code, I would be very appreciative.

Bob

[import]uid: 45029 topic_id: 9573 reply_id: 309573[/import]

Which part isn’t working. I just ran it in the simulator ( naturally faking the shake ) and it works fine. Are you getting any errors printed out? [import]uid: 5833 topic_id: 9573 reply_id: 34956[/import]

Hi Graham

Thanks for the response. I am learning quickly that not only detail is ‘king’ in this coding malarkey but concentration can’t be very far behind! Having only used Corona for 5 days, I am ‘missing’ things that are fairly obvious e.g. making sure that the 3 coloured .pngs MATCH the 3 coloured images loaded!

However, I am loving what for me is a HUGE learning curve.

Thanks again, it is re-assuring to know that there are people out there for whom my problems are
not problems at all.

Bob [import]uid: 45029 topic_id: 9573 reply_id: 34963[/import]

Silly mistakes are the most fun :slight_smile:

And I can assure you, this community is awesome. Fact. Whenever you have a problem there will always be someone on here that has had the same problem before and can help you. [import]uid: 5833 topic_id: 9573 reply_id: 34968[/import]

Hi Graham

Having now finished the functionality bit, I am now getting ‘cocky’ and considering adding an audio file to narrate one sentence of text on each of the random images e.g. when a text sentence on a particular page is pressed it is read aloud. This would be 'bells ‘n’ whistles on my app, do you think this work will be too complicated for me? Honesty is OK and much appreciated.

Thanks again.

Bob [import]uid: 45029 topic_id: 9573 reply_id: 34973[/import]

I don’t think it would be that tricky at all, as long as you know how to use tap/touch event handlers and play an audio file you should be fine. [import]uid: 5833 topic_id: 9573 reply_id: 35089[/import]

Hi Graham

Thanks again for your response. Although I am very new (8 days) to coding in general not only Corona, I am managing to get my head round the various relationships between things. I now have my buttons working and a random image selected on shake in the game. However, this audio bit is causing me concern because if a random image (.png) is being selected, does this mean that rather than having my 50 or 360 images as 1.png, 2.png…77.png etc I will have to use a different .lua file (Director Class) for each of the e.g. 360 random images so that a tap/touch event and audio file can be attached to each random image selected?

Any advice would be appreciated.

Bob [import]uid: 45029 topic_id: 9573 reply_id: 35238[/import]

Sorry for the delayed reply, you should be able to use one Director screen for all images. Something like this:

[code]

local images = {}
images[1].image = “image1.png”
images[1].sound = “sound1.ogg”

images[2].image = “image2.png”
images[2].sound = “sound2.ogg”

local function onTap( event )

local image = event.target
local sound = image.sound

– This is where you can play the sound file,
end

local function onShake( event )

if event.isShake then

local r = math.random( 1, #images )
local image = display.newImage( images[r].image, 0, 0)
image.sound = images[r].sound
image:addEventListener( “tap”, onTap )

end

end

Runtime:addEventListener(“accelerometer”, onShake); [import]uid: 5833 topic_id: 9573 reply_id: 35770[/import]

Hi Graham

Thanks for once again taking the trouble to consider a solution for me, I do appreciate it!

I will try/play around with it using your suggestion. One question, I assume I should list the total number of images/sounds? Apologies if this sounds really obvious!
e.g.

local images = {}
images[1].image = “image1.png”
images[1].sound = “sound1.ogg”

images[2].image = “image2.png”
images[2].sound = “sound2.ogg”

images[3].image = “image3.png”
images[3].sound = “sound3.ogg”

images[59].image = “image59.png”
images[59].sound = “sound59.ogg”

etc

This is like having a personal ‘ASK JEEVES’ butler style guru!

Bob [import]uid: 45029 topic_id: 9573 reply_id: 35838[/import]

You will want to list them all however assuming all your sounds and images follow the same naming format you will be able to do something like this:

[code]

local images = {}

local totalImages = 60

for i = 1, totalImages, 1 do
images[i].image = “image” … i … “.png”
images[i].sound = “sound” … i … “.ogg”
end

[/code] [import]uid: 5833 topic_id: 9573 reply_id: 35867[/import]

Something to note, don’t forget to dispose (audio.dispose()) of your sound files when your done with them else you can cause memory problems when changing scenes. [import]uid: 33866 topic_id: 9573 reply_id: 35871[/import]

Hi cl-apps

Thanks for this advice, apologies but what does it mean? At what point where in the code should this appear and why?

Thanks again

Bob [import]uid: 45029 topic_id: 9573 reply_id: 36143[/import]

Just as your changing scene, so just before: director:changeScene(“NextScene”, “fade”)

You want to include something like : audio.dispose(images[1].sound) or in your case you might have to create a for loop to get rid of them all from your table:

[lua]local function endGame ()

for i = 1, #images do

audio.dispose(images[i].sound) – Get rid of the audio, freeing up memory

end

director:changeScene(“NextScene”, “fade”)

end[/lua]

Not checked this to see if it works properly but its the general idea of freeing up memory when you have finished with it else you can easily create an app that will crash a lot on the device.

Hope this helps a little.

[import]uid: 33866 topic_id: 9573 reply_id: 36261[/import]

Hi cl-apps

Thanks for this. I will use the code that Graham provided to get the function bit right (random images with sound on tap/touch) and then explore applying your suggestion in the right place (hopefully).

Bob [import]uid: 45029 topic_id: 9573 reply_id: 36271[/import]

Hi Graham

The code below works perfectly in terms of buttons, shake feature to select random image.
local background = display.newImage (“shake.png”)
localGroup:insert(background)
–> This sets the background

local img = { “1.png”, “2.png”, “3.png”, “4.png”, “5.png”, “6.png” };

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

return true;
end

Runtime:addEventListener(“accelerometer”, onShake);
return localGroup
end
However, when I try to add a sound file using the code you provided it doesn’t function after the shake gesture. I appreciate I may be missing something very basic or that I just don’t understand but can you see where I am going wrong. Here is what I have just now, the

local images = {}
images[1].image = “1.png”
images[1].sound = “sound1.mp3”

images[2].image = “2.png”
images[2].sound = “sound2.mp3”

images[3].image = “3.png”
images[3].sound = “sound3.mp3”

local function onTap( event )

local image = event.target
local sound = image.sound

– This is where you can play the sound file,

end

local function onShake( event )

if event.isShake then

local r = math.random( 1, #images )
local image = display.newImage( images[r].image, 0, 0)
image.sound = images[r].sound
image:addEventListener( “tap”, onTap )

end

end

Runtime:addEventListener(“accelerometer”, onShake);
Thanks
Bob [import]uid: 45029 topic_id: 9573 reply_id: 36576[/import]

If possible, could you zip up the whole project and send it to me? I will be able to take a full look at it if you want then. Email: graham AT grahamranson DOT co DOT uk [import]uid: 5833 topic_id: 9573 reply_id: 36577[/import]

@bob8,
did you check the samples on code exchange, there is a sample that lets you highlight text that is being played with audio, like in a read aloud story book. The next thing that you might ask for, so head over and have a peek on Code Exchange.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 9573 reply_id: 36581[/import]

Hi jayantv

Thanks for the advice.

Bob [import]uid: 45029 topic_id: 9573 reply_id: 36589[/import]

Being new to this I am not sure if it is etiquette/Ok to do this but I just wanted to say a very big public thanks to Graham Ranson for helping me with my project! Despite feeling really pleased at my very first coding efforts over the past 10 days or so, I would never have managed to work out what was required to allow me to progress my app creation to it’s current level. Graham you are a star!

Thanks also to everyone who tries to make this forum a place where newcomers are welcomed! [import]uid: 45029 topic_id: 9573 reply_id: 36590[/import]

Hey Bob, you are very welcome :slight_smile: [import]uid: 5833 topic_id: 9573 reply_id: 36631[/import]