Function gets called twice; once in a while. [SAMPLE (CODE/PROJECT) INCLUDED]

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!!

A quick fix would be to put a flag called “isTransitioning” in and then only change scene if that is false, and then set it to true. Something like this:

[lua]

local isTransitioning = false

local function onCollision(event)

if event.phase == “began” and not isTransitioning then

isTransitioning = true

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

[/lua]

Hello, Thanks for your answer! It works!

I was wondering tho… Where can i find the docs for the isTransitioning? I couldn’t find it by looking on google…

Thanks Again!

Happy to help! 

“isTransitioning” is just a random variable name, it could just have well been called “isAMonkey”, however that would have been a more confusing name :slight_smile:

Ohhh haha understood !

Thanks Again!

No problem :slight_smile:

Btw… Did you like the sample code? Did it help??? If so maybe we should get the moderators to encourage devs to post small samples of their problems… It would probably help out a ton and the topic would get answered much much faster

Sample code is ALWAYS helpful :slight_smile:

A quick fix would be to put a flag called “isTransitioning” in and then only change scene if that is false, and then set it to true. Something like this:

[lua]

local isTransitioning = false

local function onCollision(event)

if event.phase == “began” and not isTransitioning then

isTransitioning = true

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

[/lua]

Hello, Thanks for your answer! It works!

I was wondering tho… Where can i find the docs for the isTransitioning? I couldn’t find it by looking on google…

Thanks Again!

Happy to help! 

“isTransitioning” is just a random variable name, it could just have well been called “isAMonkey”, however that would have been a more confusing name :slight_smile:

Ohhh haha understood !

Thanks Again!

No problem :slight_smile:

Btw… Did you like the sample code? Did it help??? If so maybe we should get the moderators to encourage devs to post small samples of their problems… It would probably help out a ton and the topic would get answered much much faster

Sample code is ALWAYS helpful :slight_smile: