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]
