Need collision detection code

Could somebody please do me a great favour and write me a small bit of code for collision detection.

I have 2 objects, “ball” and “flag”, and when they collide I need a popup to appear with a button on it.

I would be very grateful if somebody could write this could for me.
Thanks [import]uid: 116264 topic_id: 20348 reply_id: 320348[/import]

See Corona For Newbies - Part 4, it has an example of this; http://techority.com/2011/06/19/corona-for-newbies-part-4-physics/

Peach :slight_smile: [import]uid: 52491 topic_id: 20348 reply_id: 79667[/import]

Thanks! I’ve managed to add the collision detection code but I couldn’t find any information as to making a popup appear. Where could I find the code for this? [import]uid: 116264 topic_id: 20348 reply_id: 79765[/import]

A popup is something you’d code yourself - rather than printing you could display a new image for the popup, buttons, etc.

Or are you talking about a native alert? (I’ve heard people call them popups and wanted to clarify.)

Peach :slight_smile: [import]uid: 52491 topic_id: 20348 reply_id: 79808[/import]

Sorry to be unclear - I was talking about a native alert [import]uid: 116264 topic_id: 20348 reply_id: 79869[/import]

Ive managed to write the code for the native alert button there is still a problem - the buttons aren’t working. The code is:
function ball:collision (event)
if event.other.myName == “flag” then
print “Level complete”
physics.pause()
local alert = native.showAlert( “Congratulations!”, “You have completed level 1.”,
{ “Next Level”, “Menu” }, onComplete )
end
end

local function onComplete( event )
if “clicked” == event.action then
local i = event.index
if 1 == i then

director:changeScene (“level2”)
elseif 2 == i then
director:changeScene (“menu”)
end
end
end
[import]uid: 116264 topic_id: 20348 reply_id: 79899[/import]

This is a LUA ‘feature’

You need to put the onComplete function before the function ball:collision (event) in your code,( or at least have a forward declaration of the function.)
[import]uid: 108660 topic_id: 20348 reply_id: 79913[/import]

Thanks, ive rearranged the code and it works fine in the simulator, however it doesnt work when I try it on the device. Why is this? here is the code:

ball.myName = "ball"
flagpole.myName = "flag"
ball:addEventListener("collision", ball)

local function onComplete( event )
 if event.action == "clicked" then
 if event.index == 1 then
 director:changeScene ("go2")
 elseif event.index == 2 then
 director:changeScene ("menu")
 end
 end
end

function ball:collision (event)
 if event.other.myName == "flag" then
 physics.pause()
 transition.to(ball, {x = flagpole.x - 30, y = flagpole.y + 20})
 local alert = native.showAlert( "Congratulations!", "You have completed level 1.", 
 { "Next Level", "Menu" }, onComplete )
 end
end

[import]uid: 116264 topic_id: 20348 reply_id: 80150[/import]