Forward Referencing Help

Hello, I am rather new to Corona SDK, but I am trying to write the code for a pause menu… Unfortunately the script I have to resume the game doesn’t work. I have it setup so that the button “resumeMenu” runs the script “resume”… That is supposed to get rid of “pauseMenu”, and “exitMenu”… This is the code I have, and any suggestions/help would be highly appreciated.

[code]
function new()

local localGroup = display.newGroup()

local isPaused = false

– Game

local score = 0

local background = display.newImage(“images/bk-default.png”)
background.x = display.contentCenterX
background.y = display.contentCenterY
localGroup:insert(background)

local scoreText = display.newText( "Score: "… score, 0, 0, native.systemFontBold, 16 )
scoreText:setTextColor(0, 0, 0)
scoreText.x = display.contentWidth / 4
scoreText.y = display.contentHeight / 8
localGroup:insert(scoreText)

local pause = display.newImage(“images/pause.png”)
pause.x = display.contentWidth / 4
pause.y = display.contentHeight / 18
pause.alpha = .5
localGroup:insert(pause)

– Touch to go back
local touched = function( event )
print(“Screen Change”)
director:changeScene(“menu”,“fade”)
end

local paused = function( event )
local resume
if isPaused == false then
isPaused = true
local pauseMenu = display.newImage(“images/Panel.png”)
pauseMenu.x = display.contentWidth / 2
pauseMenu.y = display.contentHeight / 2
pauseMenu.alpha = .5
localGroup:insert(pauseMenu)

exitMenu= ui.newButton{
defaultSrc = “images/Button.png”,
defaultX = 160,
defaultY = 32,
overSrc = “images/Button Over.png”,
overX = 160,
overY = 32,
id = “resumeMenu”,
text = “Exit”,
font = “Helvetica”,
textColor = { 0, 0, 0, 255 },
emboss = false
}

exitMenu:addEventListener( “tap”, touched )
exitMenu.x = display.contentWidth / 2
exitMenu.y = display.contentHeight / 3

resumeMenu= ui.newButton{
defaultSrc = “images/Button.png”,
defaultX = 160,
defaultY = 32,
overSrc = “images/Button Over.png”,
overX = 160,
overY = 32,
id = “resumeMenu”,
text = “Resume”,
font = “Helvetica”,
textColor = { 0, 0, 0, 255 },
emboss = false
}

resumeMenu:addEventListener( “tap”, resume )
resumeMenu.x = display.contentWidth / 2
resumeMenu.y = display.contentHeight / 4
end
end

pause:addEventListener(“tap”, paused)

local resume = function( event )
pauseMenu:removeSelf()
exitMenu:removeSelf()
end

unloadMe = function()
end

– MUST return a display.newGroup()
return localGroup
end
[/code] [import]uid: 26946 topic_id: 13321 reply_id: 313321[/import]

try putting
[lua] local resume = function( event )
pauseMenu:removeSelf()
exitMenu:removeSelf()
end[/lua]
anywhere above [lua]resumeMenu:addEventListener( “tap”, resume )[/lua]

I recommend putting both touched() and resume() function inside the function paused().

see the below code
[lua]local localGroup = display.newGroup()
local isPaused = false

local pause= display.newText(“Pause”,100,100,nil,25)
pause.x = display.contentWidth / 4
pause.y = display.contentHeight / 18
pause.alpha = .5
localGroup:insert(pause)

local paused = function( event )
if isPaused == false then
isPaused = true
local exitMenu= display.newText(“Exit”,100,100,nil,25)
local resumeMenu= display.newText(“Resume”,100,120,nil,25)
local resume = function( event )
resumeMenu:removeSelf()
exitMenu:removeSelf()
isPaused = false
end
local touched = function( event )
print(“Screen Change”)
–director:changeScene(“menu”,“fade”)
end
resumeMenu:addEventListener(“tap”, resume)
exitMenu:addEventListener(“tap”, touched)
end
end

pause:addEventListener(“tap”, paused)[/lua] [import]uid: 71210 topic_id: 13321 reply_id: 48922[/import]

the problem is very simple, you have declared a

[lua]local resume[/lua]
and then declared it further down as

[lua]local resume = function (event)[/lua]

if you want to forward declare, then do not redeclare it.
so use

[lua]local resume
resume = function(event)
end[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13321 reply_id: 48944[/import]

ha ha I din’t notice that… [import]uid: 71210 topic_id: 13321 reply_id: 48946[/import]

Thanks guys… I guess I didn’t completely understand the tutorial I read… [import]uid: 26946 topic_id: 13321 reply_id: 48956[/import]