collision error

some how when the first time  object one and object 2 collide instead displaying first text “you won” i get the second text on display " Game over …" 

this only happen only first time when they collide rest works fine … don’t no why this happening but i need your help

code below :

[lua]ocal function onGlobalCollision( event )
if ( event.phase == “began” ) then
if (event.object1.myName == “happy” and event.object2.myName== “cake”) then
local textW = display.newText(“You Won”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)
textW:setTextColor(230, 250, 0)
happy:removeSelf()
cake:removeSelf()

– end
timer.performWithDelay(1000, function() textW:removeSelf(); text = nil; cake = nil end)
timer.performWithDelay(1100, function()

createHappy();
createCake();
–moveCake()

angry.x = 880
angry.y = 600

angry2.x = 830
angry2.y = 520

cake.x = 730
cake.y = 420
end)

else

local text = display.newText(“Game Over! Try Again.”, display.contentCenterX, display.contentCenterY, native.systemFontBold, 50)
text:setTextColor(0, 250, 260)
–grp:insert(text)
–grp:insert(textW)
scoreTxt.alpha = 0
score = 0
scoreTxt.text = "score: "…score
happy:removeSelf()
cake:removeSelf()

timer.performWithDelay(1000, function() text:removeSelf(); text = nil; cake = nil end)
timer.performWithDelay(1100, function()

–reset things after the collision

–scoreTxt.text = "score: "…score

createHappy();
createCake();
–moveCake()

angry.x = 880
angry.y = 600

angry2.x = 830
angry2.y = 520

cake.x = 330
cake.y = 420

end)
end
end
end

Runtime:addEventListener(“collision”, onGlobalCollision)[/lua]

My guess would be that you are checking for event.object1.name == “happy” and event.object2.name == “cake” when in fact the names might be reversed (1 might be cake and 2 might be happy).  I would print some info to the console at the start of the collision event to see what the names of the event objects are.
 

It could be as simple as changing this:

if (event.object1.myName == “happy” and event.object2.myName== “cake”) then
 

to this:

 

if (event.object1.myName == “happy” and event.object2.myName== “cake”) or (event.object1.myName == “cake” and event.object2.myName== “happy”) then

 

Hope this helps. 

@alanmthomas your guess was right …

thanks

My guess would be that you are checking for event.object1.name == “happy” and event.object2.name == “cake” when in fact the names might be reversed (1 might be cake and 2 might be happy).  I would print some info to the console at the start of the collision event to see what the names of the event objects are.
 

It could be as simple as changing this:

if (event.object1.myName == “happy” and event.object2.myName== “cake”) then
 

to this:

 

if (event.object1.myName == “happy” and event.object2.myName== “cake”) or (event.object1.myName == “cake” and event.object2.myName== “happy”) then

 

Hope this helps. 

@alanmthomas your guess was right …

thanks