Local Collision Problem

I am developing my first mobile application with Corona SDK. This is the rule of my game: I have this ball and when it collides with the end of the maze, it will show a dialog box that you passed the level. I tried adding a collision listener at the finish line, but it doesn’t work. The code I used DOES show the dialog box, but it happens that the dialog box popups when the ball collides with EVERY border I have (not the finish line).
Here is the code I have so far(part of it):

[code]
local ball = display.newImage(“ball3.png”, _W, _H);
ball:setReferencePoint(display.CenterReferencePoint);
ball.x = 350; ball.y = 100;

local borderend = display.newRect( 65, 55, 20, 100 )
borderend:setFillColor( 0, 0, 0, 0) – make invisible
physics.addBody( borderend, “static”, borderBodyElement )

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
local alert = native.showAlert( “You Passed The level!”, “”, { “Level2”, “Main Menu” }, onBackComplete )

end
end

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

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

–Can anyone help me show the dialog box when the ball collides with the finish line but not every border I have? [import]uid: 44110 topic_id: 15551 reply_id: 315551[/import]

This is because your ball is listening for any kind of collision.

You need to make it specific :wink:

Luckily I have a tutorial that is perfect for this, very simple and easy to follow - check this out; http://techority.com/2011/06/19/corona-for-newbies-part-4-physics/

Let me know how you go.

Peach :slight_smile: [import]uid: 52491 topic_id: 15551 reply_id: 57469[/import]

Thanks Peach
I changed the code and it works (shows dialog box when collided to the end)
But I got another problem: when the ball collides with the end, it shows the dialog box EVERY TIME it collides with the end.
I want it to show the dialog box when it collides with the end once, not every time it collides.
What should I do?

My code so far: (Still part of it because there is about 400 lines in total)

[code]
local ball = display.newImage(“ball3.png”, _W, _H);
ball:setReferencePoint(display.CenterReferencePoint);
ball.x = 350; ball.y = 100;

local borderend = display.newRect( 65, 55, 20, 100 )
borderend:setFillColor( 0, 0, 0, 0) – make invisible
physics.addBody( borderend, “static”, borderBodyElement )

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
local alert = native.showAlert( “You Passed The level!”, “”, { “Level2”, “Main Menu” }, onBackComplete )

end
end

borderend.collision = onLocalCollision
borderend:addEventListener( “collision”, borderend ) [import]uid: 44110 topic_id: 15551 reply_id: 57495[/import]

i suggest to remove collision listener upon collision, this way your collision will fire only once [import]uid: 16142 topic_id: 15551 reply_id: 57502[/import]

@darkconsoles good advice but didn’t work [import]uid: 44110 topic_id: 15551 reply_id: 57508[/import]

working perfectly, collision fires only once
[lua]local physics = require(“physics”)
physics.start()
local ball = display.newCircle(0,0,30);
ball:setReferencePoint(display.CenterReferencePoint);
ball.x = 100; ball.y = 100;
physics.addBody(ball)

local borderend = display.newRect( 0, 0, 200, 10 )
borderend.y = 300
physics.addBody( borderend, “static”)

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print(event.phase)
borderend:removeEventListener(“collision”, borderend)
end
end

borderend.collision = onLocalCollision
borderend:addEventListener( “collision”, borderend )[/lua] [import]uid: 16142 topic_id: 15551 reply_id: 57510[/import]

Works on Corona Simulator

I forgot to tell you that it works on the corona simulator but it doesn’t work on the Xcode simulator [import]uid: 44110 topic_id: 15551 reply_id: 57512[/import]

Tested in Xcode simulator build using Corona version .591 (the latest stable release), using the following code, I do not see a problem.

[lua]local physics = require(“physics”)
physics.start()

local ball = display.newCircle(0,0,30);
ball:setReferencePoint(display.CenterReferencePoint);
ball.x = 100; ball.y = 100;
physics.addBody(ball)

local borderend = display.newRect( 0, 0, 200, 10 )
borderend.y = 300
physics.addBody( borderend, “static”)

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print(event.phase)
borderend:removeEventListener(“collision”, borderend)
local alert = native.showAlert( “You Passed The level!”, “”, { “Level2”, “Main Menu” }, onBackComplete )

end
end

borderend.collision = onLocalCollision
borderend:addEventListener( “collision”, borderend )[/lua]

Peach [import]uid: 52491 topic_id: 15551 reply_id: 58532[/import]

Many and many thanks for your help on my problem. The problem has troubled me for six months. So excited for your answer! Sorry for the typo on your site [import]uid: 44110 topic_id: 15551 reply_id: 58658[/import]

Hey there,

No sure what typo you mean, but it’s all good :wink:

I hope your problem is now resolved, test it on device and see - but in the Xcode simulator that code worked totally fine.

Peach :slight_smile: [import]uid: 52491 topic_id: 15551 reply_id: 58686[/import]