So i have two objects that move and one object in the middle… So what happens is when one of the objects hits the middle object then scene changes… But sometimes both moving objects hit the middle object at the EXACT SAME time and the GoTo Scene gets called TWO times and not ONCE…
How can i fix this??
Here is sample code showing my problem…
You can put this sample code into a new main.lua file and restart your simulator and you’ll see my sample project.
local physics = require("physics") physics.start() local middleBox = display.newRect( display.contentCenterX, display.contentCenterY, 50, 50 ) middleBox:setFillColor( 1, 0, 0 ) physics.addBody( middleBox, "static" ) middleBox.myName = "middleBox" local function onCollision(event) if event.phase == "began" then if event.target.myName == "middleBox" and event.other.myName == "topBox" then print("GoToScene") elseif event.target.myName == "middleBox" and event.other.myName == "bottomBox" then print("GoToScene") end end end middleBox:addEventListener( "collision", onCollision ) local topBox = display.newRect( display.contentCenterX, display.contentCenterY - 200, 30, 30 ) topBox:setFillColor( 0, 0, 1 ) physics.addBody( topBox, "dynamic" ) topBox.gravityScale = 0 topBox:setLinearVelocity( 0, 200 ) topBox.myName = "topBox" local bottomBox = display.newRect( display.contentCenterX, display.contentCenterY + 200, 30, 30 ) bottomBox:setFillColor( 0, 0, 1 ) physics.addBody( bottomBox, "dynamic" ) bottomBox.gravityScale = 0 bottomBox:setLinearVelocity( 0, -200 ) bottomBox.myName = "bottomBox"
Thanks for any help!!
