For loop to create 3 buttons with a different sound for each button.

Summary: I have used a for loop to automate the creation of 3 squares that respond to a touch event. The touch event is to trigger a different sound for each square. When testing the code, sound is not triggered when pressing the square if passed as a variable generated in the for loop. However, when hard-coding the variable for the soundload event, the sound plays.

Question: Why would audio.play(sound) work when variable (e.g. “sound1”) is written into the function call but not when using a generated variable? As a side note - Would there be a better way to write the following code?

Thank you for your insights.
– load 3 sound files.
local sound1 = audio.loadSound( “sound1.wav” )
local sound2 = audio.loadSound( “sound2.wav” )
local sound3 = audio.loadSound( “sound3.wav” )

x = 25
for i = 1, 3, 1 do
square = “square” … i
sound = “sound” … i
createSquares(square, sound, x)
x = x + 90
end

– Create 3 squares
function createSquares(square, sound, x)
square = display.newRect( 20, 0, 150, 150)
square:addEventListener( “tap”, square )
local myListener = function ( event )
if ( event.phase == “began” )
– Do something
audio.play(sound) – When hard-coding the variable (e.g. “sound1”), sound plays. When using the generated variable, no sound plays.
elseif ( event.phase == “moved” )
– Do something
elseif ( event,phase == “ended” )
– Do something
end
end [import]uid: 32833 topic_id: 5827 reply_id: 305827[/import]

because sound in your listener is a string “sound1”, not the variable sound1

[import]uid: 6645 topic_id: 5827 reply_id: 19999[/import]

The thing is - I am trying to dynamically create the strings (sound1, sound2, sound3) that point to variables sound1, sound2, sound3.

  • In the for loop, I create the string (In this example, sound1) and assign it to variable sound:

sound = “sound” … i

  • This variable gets passed to function createSquares(square, sound, x)

This is the part I am confused about. You mention that I have a string in the listener as sound1. But I need to use that string to “point to” the variable “sound1” that references the loaded sound file.

I am unsure as to how to do this.

Basically what I am trying to do is: dynamically generate a set of 3 strings (sound1, sound2, sound3) and then have these dynamically point to the 3 sound file variables if this makes any sense.

Thanks.

[import]uid: 32833 topic_id: 5827 reply_id: 20005[/import]

use an array

[lua]local sounds = {}

for i=1, 5, 1 do
sounds[i] = audio.loadSound(“sound”…i…".wav")
end

num = 3

audio.play(sounds[num]) – plays sound 3[/lua] [import]uid: 6645 topic_id: 5827 reply_id: 20051[/import]

Thank you for the info on the array.

What I am trying to do is:

  1. Create three buttons using a for loop (Succeeded)
  2. Have these 3 buttons’ x, y coordinates dynamically modified as the for loop iterates (Succeeded)

Is it possible to use the same listener with multiple objects to dynamically modify the properties of each object in an “Object Oriented” fashion?

For example, I have create square1, square2, square3

Is it possible to use all 3 objects with the same listener and substitute the object’s name dynamically?

Thanks. [import]uid: 32833 topic_id: 5827 reply_id: 20087[/import]

please post code inside [lua] [/lua] tags [import]uid: 6645 topic_id: 5827 reply_id: 20106[/import]

[lua]local squares = {}

function onTouch(event)

local id = event.target.id
squares[id].alpha=0.5

end

for i=1, 3, 1 do

local square = display.newImage(“square.png”)
square.id = i
square:addEventListener(“touch”, onTouch)
squares[i] = square

end[/lua] [import]uid: 6645 topic_id: 5827 reply_id: 20107[/import]

Let me demonstrate what I currently have and what would be the ideal situation (Code below).

As you can see, I have 3 squares, 3 event functions and 3 listeners.

This can’t at all be efficient. What if one wanted to have, say, 10 squares? One would have to go in and change each function, each listener and each square!

I am not seeing this, but there really must be a better way to do this. So this is what I was attempting to do using for loops. However, it’s just not working out and so I am stuck with the following non-elegant approach.

From my very brief knowledge of object oriented programming, this can’t be the correct way to do this (What if someone wanted 15 buttons?). If you can point in the right direction I very much appreciate it. Thanks for your help.

[lua]local beepSound = audio.loadSound( “c3.wav” )
local beepSound2 = audio.loadSound( “d3.wav” )
local beepSound3 = audio.loadSound( “e3.wav” )

local square1 = display.newRect( 20, 0, 100, 100 )
square1:setFillColor(205,150,255)
square1.alpha = 0.8
square1:setReferencePoint(display.LeftReferencePoint)

local square2 = display.newRect( 110, 0, 100, 100 )
square2:setFillColor(155,150,255)
square2.alpha = 0.8

local square3 = display.newRect( 200, 0, 100, 100 )
square3:setFillColor(100,150,255)
square3.alpha = 0.8

local w,h = display.contentWidth, display.contentHeight

function square1:touch( event )
if(event.phase == “began”) then
audio.play( beepSound );
transition.to( square1, { time=50, alpha=0.3 });
elseif(event.phase == “moved”) then
transition.to( square1, { time=50, alpha=0.3 });
elseif(event.phase == “ended”) then
transition.to( square1, { time=100, alpha=0.8 });
end
end

function square2:touch( event )
if(event.phase == “began”) then
audio.play( beepSound2 );
transition.to( square2, { time=50, alpha=0.3 });
elseif(event.phase == “moved”) then
transition.to( square2, { time=50, alpha=0.3 });
elseif(event.phase == “ended”) then
transition.to( square2, { time=100, alpha=0.8 });
end
end

function square3:touch( event )
if(event.phase == “began”) then
audio.play( beepSound3 );
transition.to( square3, { time=50, alpha=0.3 });
elseif(event.phase == “moved”) then
transition.to( square3, { time=50, alpha=0.3 });
elseif(event.phase == “ended”) then
transition.to( square3, { time=100, alpha=0.8 });
end
end

square1:addEventListener( “touch”, square )
square2:addEventListener( “touch”, square2 )
square3:addEventListener( “touch”, square3 )[/lua] [import]uid: 32833 topic_id: 5827 reply_id: 20088[/import]

Corrected code (Tried doing an “edit” and editing existing code, but forum drop-down box is not populate and will not let me save the form without populating the “forum” dropdown")

[lua]-- load 3 sound files.
local sound1 = audio.loadSound( “sound1.wav” )
local sound2 = audio.loadSound( “sound2.wav” )
local sound3 = audio.loadSound( “sound3.wav” )

x = 25

for i = 1, 3, 1 do
square = “square” … i
sound = “sound” … i
createSquares(square, sound, x)
x = x + 90
end

– Create 3 squares
function createSquares(square, sound, x)
square = display.newRect( x, 0, 150, 150)
square:addEventListener( “touch”, square )
local myListener = function ( event )
if ( event.phase == “began” )
– Do something
audio.play(sound) – When hard-coding the variable (e.g. “sound1”), sound plays. When using the generated variable, no sound plays.
elseif ( event.phase == “moved” )
– Do something
elseif ( event,phase == “ended” )
– Do something
end
end[/lua] [import]uid: 32833 topic_id: 5827 reply_id: 19975[/import]

Thank you jmp909 - I appreciate your help.
Also updated previous post adding the lua tags to code.

So, based on your code, I am seeing that each displayObject has an id property that gives one the ability to access a unique instance of this object.

Thank you for your help!

As a side note: The Corona SDK documentation is very useful. However, I am looking forward to an enterprising guru(s) coming out with a “Corona SDK for Beginners/Programmers” book in the future hopefully. :slight_smile:

Or perhaps more videos on Corona SDK (The videos I’ve seen so far have been very helpful). [import]uid: 32833 topic_id: 5827 reply_id: 20157[/import]

Hi Jmp,

I am new in Lua.

I appreciate to read your code.

Its help full for me but i have 5 rectangle object and i want to work with only one listener how would its possible, like we have one click event of 3 button and we have doing with sender object find id and do operation on that.

one more help required how to call my user defined function.?

Thankx
Krunal

[import]uid: 33757 topic_id: 5827 reply_id: 21375[/import]