Cought in a loop programming problem..

Hello,
I’m new to Corona, coming from AS3 and I still don’t get some of the coding practice. I’m stuck with a problem of “loop” or don’t know how to say, because a function has to be declared higher in the code that when you use it, apparently. I’ll explain here:

I have a function to create a btn:
[blockcode]
local createBtn = function(url)
if(myBtn ~= nil) then
myBtn:removeSelf()
end

myBtn = ui.newButton{default = url…".png", over =url…"_over.png", onRelease = pressPlay}
myBtn.x = 250
myBtn.y = 150
localGroup:insert(myBtn)
end
[/blockcode]

And I have a function pressPlay that when click, I want it to remove the btn, and replace it by another (which is now a “pause” btn.)

[blockcode]
local pressPlay = function()
if fSoundPlaying then
media.pauseSound()
fSoundPlaying = false
fSoundPaused = true
createBtn(“btnPlay”)
else
fSoundPlaying = true
media.playSound()
createBtn(“btnPause”)
end
end
[/blockcode]

This does not work as pressPlay should be before the createBtn in my code so the listener works when creating the btn. But then if I do that, it won’t work either because createBtn would not be before pressPlay… hummmm
I probably don’t get yet the best practice of coding with Lua… Any help would be great!
thanks,
seb
[import]uid: 29594 topic_id: 6606 reply_id: 306606[/import]

[lua]local myBtn – forward reference
local pressPlay – forward reference

local createBtn = function(url)
if(myBtn ~= nil) then
myBtn:removeSelf()
end

myBtn = ui.newButton{default = url…".png", over =url…"_over.png", onRelease = pressPlay}
myBtn.x = 250
myBtn.y = 150
localGroup:insert(myBtn)
end
pressPlay = function() – it’s still local
if fSoundPlaying then
media.pauseSound()
fSoundPlaying = false
fSoundPaused = true
createBtn(“btnPlay”)
else
fSoundPlaying = true
media.playSound()
createBtn(“btnPause”)
end
end[/lua] [import]uid: 6645 topic_id: 6606 reply_id: 22948[/import]

Thank you! Ok now I got it! :slight_smile: [import]uid: 29594 topic_id: 6606 reply_id: 22951[/import]