Change Scene Problem

I am making a game with two scenes, the menu and the game. The menu has one button, the start button. The start button changes the scene to the game scene using director. In the game, I move around a character to try and avoid falling objects for as long as I can. When I hit a falling object, it goes back to the menu. The problem is that when it goes back to the menu, the start button doesn’t work anymore. But the start button always works the first time. Here is the code for my menu:

module(..., package.seeall)  
  
function new()  
local localGroup = display.newGroup()  
  
local startbutton = display.newImage("Button.gif")  
startbutton.x = display.contentWidth/2  
startbutton.y = display.contentHeight/2  
localGroup:insert(startbutton)  
local function startgame (event)  
if event.phase == "ended" then  
director:changeScene ("game")  
end  
end  
  
startbutton:addEventListener("touch",startgame)  
  
return localGroup  
end  

And here is the code for my game scene:

[code]
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local physics = require(“physics”)
physics.start()
grav = 5
physics.setGravity(0,grav)
–DRAGGING

local function printTouch( event )
if event.target then
local bounds = event.target.stageBounds
print( “event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
end
end

local function onTouch( event )
local t = event.target

– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
printTouch(event)

local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true

end

– Iterate through arguments array and create rounded rects (vector objects) for each item

character = display.newImage(“Character.gif”)
character.x = display.contentWidth/2
character.y = 400
– Make the character instance respond to touch events
character:addEventListener( “touch”, onTouch )
localGroup:insert(character)

– listener used by Runtime object. This gets called if no other display object
– intercepts the event.
local function printTouch2( event )
print( “event(” … event.phase … “) (”…event.x…","…event.y…")" )

end

Runtime:addEventListener( “touch”, printTouch2 )
physics.addBody( character, “static”, { friction=0.5, bounce=0.3 } )

–FALLLING OBJECTS
Fallingobject = display.newImage(“Fallingobject.gif”)
localGroup:insert(Fallingobject)
Fallingobject.x = -500
function dropping()
Fallingobject = display.newImage(“Fallingobject.gif”)
physics.addBody( Fallingobject, { density=0.9, friction=0.3, bounce=0.3} )
Fallingobject.x = math.random(display.contentWidth)
Fallingobject.y = -100
localGroup:insert(Fallingobject)
end

function increasing()
grav = grav + 5
physics.setGravity(0,grav)
dropall = timer.performWithDelay(300,dropping,10)
end
speedincrease = timer.performWithDelay(3000,increasing,0)

–COLLISION DETECTION
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

timer.cancel(dropall)
timer.cancel(speedincrease)

director:changeScene(“menu”)
end
end

character.collision = onLocalCollision
character:addEventListener( “collision”, character )

Fallingobject.collision = onLocalCollision
Fallingobject:addEventListener( “collision”, Fallingobject )
return localGroup
end

[/code] [import]uid: 38001 topic_id: 11140 reply_id: 311140[/import]

Are any errors being printed in the terminal?

Peach [import]uid: 52491 topic_id: 11140 reply_id: 40517[/import]

It doesn’t look like you are removing the eventListener for the start button. Try removing it in the startgame function before changing scenes with director and see if that helps. I would make sure you are removing all eventListeners in any scene before changing scenes since director doesn’t remove eventListeners for you (unless you modified it to do so). [import]uid: 27965 topic_id: 11140 reply_id: 40528[/import]

There are no errors being printed in the terminal.

I tried removing the eventListener for the start button, but it didn’t do anything. I just want to make sure I did it write. Here is what I did:

[code]
local function startgame (event)
if event.phase == “ended” then
startbutton:removeEventListener(“touch”,startgame)
director:changeScene (“game”)
end
end

startbutton:addEventListener(“touch”,startgame)
[/code] [import]uid: 38001 topic_id: 11140 reply_id: 40583[/import]

I just found something out. If I play the game but I don’t move my character and I just let it get hit by the object, the scene will change back to the main menu and I can go back to the game again. But as soon as I move my character and I lose, I go to the menu, but I can’t go back to the game. Does this help you figure out the problem? [import]uid: 38001 topic_id: 11140 reply_id: 40602[/import]

I actually copied your original code to a new Corona project and ran it and it works just fine on my system (Mac with OSX 10.6) although I haven’t tried it on a device. It starts, I tap the start button, scene changes and a collision happens, scene changes back to start button and I can tap the start button and start over again. I tried it several times and it works. Are you running on a Mac or a Windows machine? I wish I was running in to the same problem so I could help you fix it but I am not seeing any problems. Sorry. [import]uid: 27965 topic_id: 11140 reply_id: 40604[/import]

Well that is strange! I am on a Mac with OSX 10.6 also. Did you move the character or did you just let a falling object hit it? [import]uid: 38001 topic_id: 11140 reply_id: 40609[/import]

I tried everything I could think of; didn’t move player and let the objects hit, moved player and let the objects hit, moved player and let the objects hit while still touching player. I couldn’t reproduce the problem you describe?! Very frustrating! What version of Corona are you using? I’m using build #534. The only thing I changed with your code was I replaced the images with vector graphics (since I didn’t have any images to use) but I don’t think that should change anything. I can’t work on this anymore right now but I will see if I can look at it some more later tonight. I am frustrated for you and it’s going to annoy me until we can figure it out. [import]uid: 27965 topic_id: 11140 reply_id: 40612[/import]

Let me give you my code again. I have changed it a little bit since the last time, but the problem is still happening. Maybe I changed something before I posted the code. I don’t know. Anyways, here is my new code for my game. Could you try and see if this works for you or not? The code for my menu is still the same. Thanks for your help!

module(..., package.seeall)  
function new()  
local localGroup = display.newGroup()  
local physics = require("physics")  
physics.start()  
grav = 5  
physics.setGravity(0,grav)  
  
------------------------------------------------------------------------------  
-- DRAGGING  
------------------------------------------------------------------------------  
  
local function onTouch( event )  
 local t = event.target  
   
 -- Print info about the event. For actual production code, you should  
 -- not call this function because it wastes CPU resources.  
 -- printTouch(event)  
   
 local phase = event.phase  
 if "began" == phase then  
 -- Make target the top-most object  
 local parent = t.parent  
 parent:insert( t )  
 display.getCurrentStage():setFocus( t )  
   
 -- Spurious events can be sent to the target, e.g. the user presses   
 -- elsewhere on the screen and then moves the finger over the target.  
 -- To prevent this, we add this flag. Only when it's true will "move"  
 -- events be sent to the target.  
 t.isFocus = true  
   
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
 elseif t.isFocus then  
 if "moved" == phase then  
 -- Make object move (we subtract t.x0,t.y0 so that moves are  
 -- relative to initial grab point, rather than object "snapping").  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
 end  
 end  
   
 -- Important to return true. This tells the system that the event  
 -- should not be propagated to listeners of any objects underneath.  
 return true  
  
  
  
end  
   
-- Iterate through arguments array and create rounded rects (vector objects) for each item  
  
 character = display.newImage("character.gif")  
 character.x = display.contentWidth/2  
 character.y = 400  
 -- Make the character instance respond to touch events  
 character:addEventListener( "touch", onTouch )  
 localGroup:insert(character)  
  
   
-- listener used by Runtime object. This gets called if no other display object  
-- intercepts the event.  
physics.addBody( character, "static", { friction=0.5, bounce=0.3 } )  
  
------------------------------------------------------------------------------  
-- DROPPING  
------------------------------------------------------------------------------  
  
fallingObject = display.newImage("fallingObject.gif")  
localGroup:insert(fallingObject)  
fallingObject.x = -500  
function dropping()  
fallingObject = display.newImage("fallingObject.gif")  
physics.addBody( fallingObject, { density=0.9, friction=0.3, bounce=0.3} )  
fallingObject.x = math.random(display.contentWidth)  
fallingObject.y = -100  
localGroup:insert(fallingObject)  
end  
  
function increasing()  
grav = grav + 0.2  
physics.setGravity(0,grav)  
dropall = timer.performWithDelay(300,dropping,10)  
end  
speedincrease = timer.performWithDelay(3300,increasing,0)  
  
------------------------------------------------------------------------------  
-- COLLISION  
------------------------------------------------------------------------------  
  
local function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
   
 timer.cancel(dropall)  
 timer.cancel(speedincrease)  
  
  
 director:changeScene("menu")  
 end  
end  
   
character.collision = onLocalCollision  
character:addEventListener( "collision", character )  
  
fallingObject.collision = onLocalCollision  
fallingObject:addEventListener( "collision", fallingObject )  
return localGroup  
end  

Oh and also how do I find out my version of Corona? [import]uid: 38001 topic_id: 11140 reply_id: 40613[/import]

Okay, tried your new code and still the same result, still no problems. To find out what version of Corona your running when you open up the simulator click on the “Corona Simulator” drop down menu and click on the “About Corona” option. It will pop up with a window that will tell you the build number you are running. I will try to run it on an older build than what I’m running (build 534) when I get a chance to see if maybe there is a bug that was fixed causing the problem.

Edit: I figured out that the problem occurs on Corona SDK build 268 which is the last public (stable) release before the current one. Try downloading the latest version of Corona and see if the problem magically disappears. [import]uid: 27965 topic_id: 11140 reply_id: 40652[/import]

Thank you so much! As soon as I updated Corona, my game started to work! Wow I am so happy right now! Thanks again! :slight_smile: [import]uid: 38001 topic_id: 11140 reply_id: 40714[/import]

Ok now I have another problem. When I am playing my game and I am moving my character and then I die, it will go back to the menu, but then right back to the game again. This only happens if I am holding the character during the game right where the start button is and then I let go once I am in the menu. I’m assuming that happens because it thinks that I am pressing the button. How can I fix this? [import]uid: 38001 topic_id: 11140 reply_id: 40720[/import]