Collision Detection

I am trying to code a simple if statement that says that when the ball hits the spikes the game will bring up a menu with a reset button.

Also can you help me with ending a game when you tap a object [import]uid: 55737 topic_id: 9419 reply_id: 309419[/import]

[lua]-- setup the collision
local onCollision = function (self, event)
if event.phase == “began” and
event.other.name == “spikes” then

– do something
– put your game over code in here
– self is referring to the ball
self:removeSelf()
self = nil
end
end

[import]uid: 12455 topic_id: 9419 reply_id: 34471[/import]

I recommend you check out some of the sample codes and the Ghost vs Monster demo.

Good luck.

check out my game:
Touch Cannon – a **FREE** game

please rate :slight_smile: [import]uid: 12455 topic_id: 9419 reply_id: 34473[/import]

how do I make the spikes name? [import]uid: 55737 topic_id: 9419 reply_id: 34475[/import]

spike.name = “spikes”

you can give any object in lua and property by just writing it… like

object1.property1 = “property value”
object1.property2 = “blah”
player.health = 100

etc… [import]uid: 48521 topic_id: 9419 reply_id: 34491[/import]

Edited:

sorry, i forgot to specify that.

this should work. have fun

[lua]physics = require “physics”
physics.start(true)
physics.setDrawMode( “hybrid” )
– this is earth gravity
physics.setGravity( 0, 9.8 )
– localize the variables
local ball
local spikes
– create the collision first
local onCollision = function (self, event)
if event.phase == “began” then
if event.other.name == “spikes” then

– do something, ex: play explosion sound or show explosion

– remove the ball
– self is referring to the ball because the collision is attach to the ball
self:removeSelf()
self = nil

end
end
end

ball = display.newCircle(0,0,25)
ball:setFillColor(200,0,200,255)
ball.x = 240
ball.y = 20
ball.name = “ball”
physics.addBody(ball, “dynamic”, {density = 0.2})
ball.collision = onCollision
ball:addEventListener(“collision”, ball)

spikes = display.newRect(0,0,320,40)

spikes:setReferencePoint(display.BottomLeftReferencePoint)
spikes.x = 0
spikes.y = 480
spikes.name = “spikes”
physics.addBody(spikes, “static”, {desnity = 1})
[import]uid: 12455 topic_id: 9419 reply_id: 34504[/import]

well see, the balloons are in a function, or could I have them separte [import]uid: 39840 topic_id: 9419 reply_id: 34584[/import]

well see, the balloons are in a function, or could I have them separte [import]uid: 39840 topic_id: 9419 reply_id: 34585[/import]

well see, the balloons are in a function, or could I have them separte.
here is the code

local function spawnCrate()
local balloon = display.newImage(“red.png”);
balloon.x = math.random(600);
balloon.y = -100;
physics.addBody( balloon, { density=2.0, friction=0.5, bounce=0.3 } )
balloon:addEventListener(“touch”, removeBody)
end [import]uid: 39840 topic_id: 9419 reply_id: 34586[/import]

well see, the balloons are in a function, or could I have them separte.
here is the code

local function spawnCrate()
local balloon = display.newImage(“red.png”);
balloon.x = math.random(600);
balloon.y = -100;
physics.addBody( balloon, { density=2.0, friction=0.5, bounce=0.3 } )
balloon:addEventListener(“touch”, removeBody)
end [import]uid: 39840 topic_id: 9419 reply_id: 34587[/import]