Back Button Causes Error

Hi Guys,

I implemented a back button in my game.lua file, that should take the player back to the menu.lua file. When I hit it, it works just fine and I get redirected to the menu, but when I try to hit the play button again after having gone back to the menu, it doesn´t load the entire game (just the bullet module).

The simulator output doesn´t give me a loading error, so I don´t know what´s wrong.

I posted the code below. Any ideas ?

btw, putting the back button in or out the localGroup has no effect.
– Game.lua file

[lua]module(…,package.seeall)

function new ()

local localGroup = display.newGroup()
_W = display.contentWidth
_H = display.contentHeight
velocity = 70
halfpoint = 0

local back = display.newImageRect(“images/play_btntwo.png”, _W/10,75)
back:setReferencePoint(display.CenterReferencePoint)
back.x = _W-50
back.y = _H-100
localGroup:insert(back)

function changeScene(e)

if (e.phase == “ended”) then
director:changeScene(“menu”)

end

end

back:addEventListener(“touch”, changeScene)

local physics = require(“physics”)
physics.start( true )

local bullets = {}

local function spawnBullets(total)
for i = 1, total do
bullets[i] = bullet.newBullet()
– flag bullets for removal later
bullets[i].remove = true

end
end

local bullet = require(“bullet”)
local bar = require (“bar”)
local basket = require (“basket”)
spawnBullets(5)
return localGroup

end[/lua]
– Menu.lua file
[lua]module(…,package.seeall)
function new()

local localGroup = display.newGroup()

local bg = display.newImageRect(“images/bg.png”,_W,_H)
bg:setReferencePoint(display.CenterReferencePoint)
bg.x = _W/2
bg.y =_H/2

local play_btn = display.newImageRect(“images/play_btn.png”, _W/10,75)
play_btn:setReferencePoint(display.CenterReferencePoint)
play_btn.x = _W/2
play_btn.y = _H/2
play_btn.scene = “game”

local play_btntwo = display.newImageRect(“images/play_btntwo.png”, _W/10,75)
play_btntwo:setReferencePoint(display.CenterReferencePoint)
play_btntwo.x = _W-50
play_btntwo.y = _H-100
play_btntwo.scene = “gametwo”

local play_btnthree = display.newImageRect(“images/play_btnthree.png”, _W/10,75)
play_btnthree:setReferencePoint(display.CenterReferencePoint)
play_btnthree.x = _W-100
play_btnthree.y = _H-100
play_btnthree.scene = “gamethree”

local play_btnfour = display.newImageRect(“images/play_btnfour.png”, _W/10,75)
play_btnfour:setReferencePoint(display.CenterReferencePoint)
play_btnfour.x = _W-150
play_btnfour.y = _H-100
play_btnfour.scene = “gamefour”

local play_btnfive = display.newImageRect(“images/play_btnfive.png”, _W/10,75)
play_btnfive:setReferencePoint(display.CenterReferencePoint)
play_btnfive.x = _W-200
play_btnfive.y = _H-100
play_btnfive.scene = “gamefive”

local play_btnsix = display.newImageRect(“images/play_btnsix.png”, _W/10,75)
play_btnsix:setReferencePoint(display.CenterReferencePoint)
play_btnsix.x = _W-250
play_btnsix.y = _H-100
play_btnsix.scene = “gamesix”

local play_btnseven = display.newImageRect(“images/play_btnseven.png”, _W/10,75)
play_btnseven:setReferencePoint(display.CenterReferencePoint)
play_btnseven.x = _W-300
play_btnseven.y = _H-100
play_btnseven.scene = “gameseven”

local play_btneight = display.newImageRect(“images/play_btneight.png”, _W/10,75)
play_btneight:setReferencePoint(display.CenterReferencePoint)
play_btneight.x = _W-350
play_btneight.y = _H-100
play_btneight.scene = “gameeight”

local play_btnnine = display.newImageRect(“images/play_btnnine.png”, _W/10,75)
play_btnnine:setReferencePoint(display.CenterReferencePoint)
play_btnnine.x = _W-400
play_btnnine.y = _H-100
play_btnnine.scene = “gamenine”

local play_btnten = display.newImageRect(“images/play_btnten.png”, _W/10,75)
play_btnten:setReferencePoint(display.CenterReferencePoint)
play_btnten.x = _W-450
play_btnten.y = _H-100
play_btnten.scene = “gameten”

function changeScene(e)

if (e.phase == “ended”) then
director:changeScene(e.target.scene)
end

end

localGroup:insert(bg)
localGroup:insert(play_btn)
localGroup:insert(play_btntwo)
localGroup:insert(play_btnthree)
localGroup:insert(play_btnfour)
localGroup:insert(play_btnfive)
localGroup:insert(play_btnsix)
localGroup:insert(play_btnseven)
localGroup:insert(play_btneight)
localGroup:insert(play_btnnine)
localGroup:insert(play_btnten)

play_btn:addEventListener(“touch”, changeScene)
play_btntwo:addEventListener(“touch”, changeScene)
play_btnthree:addEventListener(“touch”, changeScene)
play_btnfour:addEventListener(“touch”, changeScene)
play_btnfive:addEventListener(“touch”, changeScene)
play_btnsix:addEventListener(“touch”, changeScene)
play_btnseven:addEventListener(“touch”, changeScene)
play_btneight:addEventListener(“touch”, changeScene)
play_btnnine:addEventListener(“touch”, changeScene)
play_btnten:addEventListener(“touch”, changeScene)

return localGroup
end[/lua] [import]uid: 126207 topic_id: 29484 reply_id: 329484[/import]

Anyone ? I tried putting everything into groups as other posts suggested , but I still get the error that on reloading the game, only the bullet shows up and not the other display objects… [import]uid: 126207 topic_id: 29484 reply_id: 118438[/import]

local back = display.newImageRect(“images/play_btntwo.png”, _W/10,75)

<<<<
Also you could try changing your
back:addEventListener(“touch”,changeScene) to
(“touch”,goback)

This code has worked for me in the past:
local backbutton = display.newImage (“backb2.png”)
backbutton.x = math.floor(display.contentWidth*0.77)
backbutton.y = 635
localGroup:insert(backbutton)

–> This places our “back” button

local function pressBack (event)
if event.phase == “ended” then
director:changeScene (“intro”)
end
end

backbutton:addEventListener (“touch”, pressBack)

Don’t forget to key out your methods so your code flows, that way you don’t get mixed up. [import]uid: 88495 topic_id: 29484 reply_id: 120319[/import]

local back = display.newImageRect(“images/play_btntwo.png”, _W/10,75)

<<<<
Also you could try changing your
back:addEventListener(“touch”,changeScene) to
(“touch”,goback)

This code has worked for me in the past:
local backbutton = display.newImage (“backb2.png”)
backbutton.x = math.floor(display.contentWidth*0.77)
backbutton.y = 635
localGroup:insert(backbutton)

–> This places our “back” button

local function pressBack (event)
if event.phase == “ended” then
director:changeScene (“intro”)
end
end

backbutton:addEventListener (“touch”, pressBack)

Don’t forget to key out your methods so your code flows, that way you don’t get mixed up. [import]uid: 88495 topic_id: 29484 reply_id: 120319[/import]