End level and return to main menu.

function showGameView:tap(e)  
 transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})  
 score = display.newText('0' .. '/' .. totalHares, 58, 6, native.systemFontBold, 16)  
 score:setTextColor(238, 238, 238)  
 prepareHares()  
end  
  
function startTimer()  
 timerSource = timer.performWithDelay(1400, showHare, 0)  
end   
  
function showHare(e)  
 if(currentHares == totalHares) then   
 alert()  
 else  
 lastHare.isVisible = false   
 local randomHole = math.floor(math.random() \* 8) + 1  
  
 lastHare = hares[randomHole]  
 lastHare:setReferencePoint(display.BottomCenterReferencePoint)  
 lastHare.yScale = 0.1  
 lastHare.isVisible = true  
  
 Runtime:addEventListener('enterFrame', popOut)  
  
 currentHares = currentHares + 1  
 end   
end  
  
function popOut(e)  
 lastHare.yScale = lastHare.yScale + 0.2  
  
 if(lastHare.yScale \>= 1) then  
 Runtime:removeEventListener('enterFrame', popOut)  
 end  
end  
  
function hareHit:tap(e)  
 audio.play(hit)  
 haresHit = haresHit + 1  
 score.text = haresHit .. '/' .. totalHares  
 lastHare.isVisible = false  
end  
  
function alert()  
 timer.cancel(timerSource)  
 lastHare.isVisible = false  
  
 local alert = display.newImage('alertBg.png')  
 alert:setReferencePoint(display.CenterReferencePoint)  
 alert.x = display.contentCenterX  
 alert.y = display.contentCenterY  
 transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3})  
  
 local score = display.newText(haresHit .. '/' .. totalHares, 220, 190, native.systemFontBold, 20)  
 score:setTextColor(204, 152, 102)  
end  
  
Main()  

How do I add the option of returning to the main menu in the game? [import]uid: 128294 topic_id: 22824 reply_id: 322824[/import]

[code]

–add a button

local function backToMenu()
scene:changeScene(“menu”)

return true
end

–add a listener to your button
button:addEventListener(“tap”, backToMenu) [import]uid: 84637 topic_id: 22824 reply_id: 91163[/import]

Applied the lines of code.

  
display.setStatusBar(display.HiddenStatusBar)  
  
local movieclip = require('movieclip')  
local bg = display.newImage('gameBg.png')  
local score  
   
local hareGroup = display.newGroup()  
local hares = {}  
   
local lastHare = {}  
   
local hit = audio.loadSound('hit.wav')  
   
   
local timerSource   
local currentHares = 0  
local haresHit = 0   
local totalHares = 30  
   
   
local Main = {}  
local startButtonListeners = {}  
local showCredits = {}  
local hideCredits = {}  
local showGameView = {}  
local prepareHares = {}  
local startTimer = {}  
local showHare = {}  
local popOut = {}  
local hareHit = {}  
local alert = {}  
  
--add a button  
   
local function backToMenu()  
 scene:changeScene("menu")  
  
 return true  
end  
   
--add a listener to your button  
button:addEventListener("tap", backToMenu)  
  
   
function prepareHares()  
   
 local hareX = {80.5, 198.5, 338.5, 70.5, 225.5, 376.5, 142.5,356.5}  
 local hareY = {11, 51, 34, 110, 136, 96, 211, 186}  
   
 for a = 1, 8, 1 do  
   
 local hare = display.newImageRect("hare.png", 63, 73) -- substitute for correct dimensions of your image  
 hare.x = hareX[a]; hare.y = hareY[a]  
 hare:addEventListener("tap", hareHit)  
 hare.isVisible = false  
 hare.id = a  
 hareGroup:insert(hare)  
   
 hares[a] = hare  
   
 end  
   
 startTimer()   
end   
   
function Main()  
 titleBg = display.newImage('titleBg.png')  
 playBtn = display.newImage('playBtn.png', display.contentCenterX - 25.5, display.contentCenterY + 40)  
 creditsBtn = display.newImage('creditsBtn.png', display.contentCenterX - 40.5, display.contentCenterY + 85)  
 titleView = display.newGroup(titleBg, playBtn, creditsBtn)  
  
 startButtonListeners('add')  
end  
  
function startButtonListeners(action)  
 if(action == 'add') then  
 playBtn:addEventListener('tap', showGameView)  
 creditsBtn:addEventListener('tap', showCredits)  
 else  
 playBtn:removeEventListener('tap', showGameView)  
 creditsBtn:removeEventListener('tap', showCredits)  
 end  
end  
  
function showCredits:tap(e)  
 playBtn.isVisible = false  
 creditsBtn.isVisible = false  
 creditsView = display.newImage('creditsView.png')  
 transition.from(creditsView, {time = 300, x = -creditsView.width, onComplete = function() creditsView:addEventListener('tap', hideCredits) creditsView.x = creditsView.x - 0.5 end})  
end  
  
function hideCredits:tap(e)  
 playBtn.isVisible = true  
 creditsBtn.isVisible = true  
 transition.to(creditsView, {time = 300, x = -creditsView.width, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})  
end  
  
function showGameView:tap(e)  
 transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})  
 score = display.newText('0' .. '/' .. totalHares, 58, 6, native.systemFontBold, 16)  
 score:setTextColor(238, 238, 238)  
 prepareHares()  
end  
  
function startTimer()  
 timerSource = timer.performWithDelay(1400, showHare, 0)  
end   
  
function showHare(e)  
 if(currentHares == totalHares) then   
 alert()  
 else  
 lastHare.isVisible = false   
 local randomHole = math.floor(math.random() \* 8) + 1  
  
 lastHare = hares[randomHole]  
 lastHare:setReferencePoint(display.BottomCenterReferencePoint)  
 lastHare.yScale = 0.1  
 lastHare.isVisible = true  
  
 Runtime:addEventListener('enterFrame', popOut)  
  
 currentHares = currentHares + 1  
 end   
end  
  
function popOut(e)  
 lastHare.yScale = lastHare.yScale + 0.2  
  
 if(lastHare.yScale \>= 1) then  
 Runtime:removeEventListener('enterFrame', popOut)  
 end  
end  
  
function hareHit:tap(e)  
 audio.play(hit)  
 haresHit = haresHit + 1  
 score.text = haresHit .. '/' .. totalHares  
 lastHare.isVisible = false  
end  
  
function alert()  
 timer.cancel(timerSource)  
 lastHare.isVisible = false  
  
 local alert = display.newImage('alertBg.png')  
 alert:setReferencePoint(display.CenterReferencePoint)  
 alert.x = display.contentCenterX  
 alert.y = display.contentCenterY  
 transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3})  
  
 local score = display.newText(haresHit .. '/' .. totalHares, 220, 190, native.systemFontBold, 20)  
 score:setTextColor(204, 152, 102)  
end  
  
Main()   
  

However, doesn’t work, it shows the level and nothing happens.

[import]uid: 128294 topic_id: 22824 reply_id: 91168[/import]

Ooops. I assumed your using storyboard ?

If not what are you using (if any) [import]uid: 84637 topic_id: 22824 reply_id: 91171[/import]

@Danny

Forgot to tell you this is off of the main.lua file. So currently not using storyboard. Is there a solution to this problem without using it. [import]uid: 128294 topic_id: 22824 reply_id: 91173[/import]

just a recommendation but if you plan on having multiple levels it would be a good idea to use storyboard or director, sure makes everything much easier and cleaner.
On another note if you want to have the levels and main menu in the same file then switching between them can be done in multiple ways, one suggestion would be to create a display group for that level and remove it along with any listeners and timers not associated with that group, put the whole level in a function and do the same with the main menu, then just remove everything in main menu then call level function and vice versa. But like i said i would highly recommend using storyboard or director. [import]uid: 126161 topic_id: 22824 reply_id: 91181[/import]

How is that done? [import]uid: 128294 topic_id: 22824 reply_id: 99939[/import]