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]
[import]uid: 38001 topic_id: 11140 reply_id: 40714[/import]