Ending A Level In a Game

I created a game in which a ball moves, I want the level to end when the ball gets inside a certain area. I have tried creating a rectangle and making the opacity .01 (to make it invisible), but I can’t get the listener to work so the level ends when the ball touches the invisible rectangle. Suggestions? [import]uid: 82872 topic_id: 28923 reply_id: 328923[/import]

Hi there,
Are you using physics or not? Any kind of collision detection (i.e. a ball inside the bounds of a rectangle) needs to be handled by the physics engine OR by a Runtime “positional listener”. The opacity of the target rectangle doesn’t matter, you just need to set up the proper detection method.

If you’re using physics, details for collision detection are in the Corona docs. If you’re not using physics, search around the forums and Code Share for examples of non-physical collision detection, of which there are a couple examples or more.

Hope this helps!
Brent
[import]uid: 9747 topic_id: 28923 reply_id: 116465[/import]

Brent’s answer is very thorough, though I’d like to add if you show some code we can likely give you some more specific guidance on this :slight_smile: [import]uid: 52491 topic_id: 28923 reply_id: 116501[/import]

If you want the level to end right when the ball touches this invisible rectangle then a simple physics collision will work. However, you may want the level to end when the ball touches the center of the rectangle to have a look as if the ball made it into the rectangle. This would require a little more code but you could still do it by creating another small rectangle inside the larger rectangle and put the collision listener on that. [import]uid: 50511 topic_id: 28923 reply_id: 116560[/import]

I looked at some of the tutorials and created an object listener. The issue I am having is, when then the crate hits the grass it comes into effect but it bounces, creating lots of unneeded information. In the terminal it says “WARNING: The simulator does not support simultaneous alerts via native.showAlert(). If an alert is shown, subsequent calls will be ignored.”

I would like it to only happen once, using the listener, an alert shows up but the buttons on the listener do not work. I am practicing with corona, I have only spent a couple hours coding so I am still new.

Here is my code, grass and crate are objects (pictures from the default game program)

[code]
–Collision detection
crate.myName = “crate”
grass.myName = “grass”

crate:addEventListener(“collision”, crate)

function crate:collision (event)
if event.other.myName == “grass” then

transition.to(ball, {x = 30, y = 20})
local alert = native.showAlert( “Congratulations!”, “You have completed level 1.”,
{ “Next Level”,“Restart”, “Menu” }, onComplete )

crate:removeEventListener(“collision”, crate)

if event.action == “clicked” then
–Next Level
if event.index == 1 then

director:changeScene(“level2”)

–Restart
elseif event.index == 2 then

director:changeScene(“level1”)

–Menu
elseif event.index == 3 then
director:changeScene(“levelmenu”)

end

end

end

end

[import]uid: 82872 topic_id: 28923 reply_id: 116640[/import]

This is solved by using the “began” phase of the collision, so it only triggers once. Understanding phases of collisions is essential and it should be you next area of study. :wink:

Best of luck, keep working at it!

Brent [import]uid: 9747 topic_id: 28923 reply_id: 116644[/import]