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]