Here’s another example very similar to the documentation on collisions at:
http://developer.anscamobile.com/content/game-edition-collision-detection
Then new function and returning a group are Director class conventions.
[lua]function new()
local physics = require “physics”
physics.start()
physics.setScale( 60 ) – a value that seems good for small objects (based on playtesting)
physics.setGravity( 0,1)
–physics.setDrawMode( “hybrid” )
local bounce = 1
local localGroup = display.newGroup()
local rBlue1 = display.newRect(localGroup, 130, 50, 50, 50)
local rGreen1 = display.newRect(localGroup, 220, 100, 50, 50)
local rRed1 = display.newRect (localGroup, 150, 300, 100,100)
rBlue1:setFillColor (0, 0, 255) ; rBlue1.myColor = “blue”
rGreen1:setFillColor(0, 255, 0) ; rGreen1.myColor = “green”
rRed1:setFillColor (255, 0 , 0)
physics.addBody( rBlue1, “dynamic”,{bounce = bounce})
physics.addBody( rGreen1, “dynamic”,{bounce = bounce})
physics.addBody( rRed1, “static” )
local function onCollision (self , event)
if event.phase == “began” then
local other = event.other
if other.myColor == “green” then
other.myColor = “blue”
other:setFillColor ( 0,0,255)
elseif other.myColor == “blue” then
other.myColor = “green”
other:setFillColor (0,255,0)
end
end
end – onCollision
rRed1.collision = onCollision
rRed1:addEventListener(“collision”, rRed1)
return localGroup
end – new
new()[/lua] [import]uid: 12635 topic_id: 5104 reply_id: 19430[/import]