–[[
drop this code in a lua file and try it. I think this is what your after. This will create 3 rectangles and a floor. The 3 rectangles are in a table and the table is passed to the function. Have your terminal open so you can see the output.
–]]
display.setStatusBar( display.HiddenStatusBar )
_W,_H = display.contentWidth, display.contentHeight
local physics = require(“physics”)
physics.start()
local myRectangle1 = display.newRect(0, 0, 150, 50)
myRectangle1.strokeWidth = 3
myRectangle1:setFillColor(140, 140, 140)
myRectangle1:setStrokeColor(180, 180, 180)
myRectangle1.x = _W / 2 * .50
myRectangle1.y = _H / 2 * .30
myRectangle1.myName = “myRectangle1”
physics.addBody(myRectangle1, “dynamic”, { density=1.0, friction=0.5, bounce=0.5 })
local myRectangle2 = display.newRect(0, 0, 150, 50)
myRectangle2.strokeWidth = 3
myRectangle2:setFillColor(140, 140, 140)
myRectangle2:setStrokeColor(180, 180, 180)
myRectangle2.x = _W / 2 * .80
myRectangle2.y = _H / 2 * .20
myRectangle2.myName = “myRectangle2”
physics.addBody(myRectangle2, “dynamic”, { density=1.0, friction=0.5, bounce=0.5 })
local myRectangle3 = display.newRect(0, 0, 150, 50)
myRectangle3.strokeWidth = 3
myRectangle3:setFillColor(140, 140, 140)
myRectangle3:setStrokeColor(180, 180, 180)
myRectangle3.x = _W / 2 * 1.20
myRectangle3.y = _H / 2 * .50
myRectangle3.myName = “myRectangle3”
physics.addBody(myRectangle3, “dynamic”, { density=1.0, friction=0.5, bounce=0.5 })
local floor = display.newRect(0, 0, _W, 20)
floor.strokeWidth = 3
floor:setFillColor(140, 140, 140)
floor:setStrokeColor(180, 180, 180)
floor.myName = “floor”
floor.x = _W / 2
floor.y = _H / 2 * 2.0
physics.addBody(floor, “static”, { density=3.0, friction=0.5, bounce=0.3 })
local variable={myRectangle1, myRectangle2, myRectangle3}
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print( self.myName … ": collision began with " … event.other.myName )
elseif ( event.phase == “ended” ) then
print( self.myName … ": collision ended with " … event.other.myName )
end
end
variable[#variable].collision = onLocalCollision
variable[#variable]:addEventListener( “collision”, variable[#variable] ) [import]uid: 80890 topic_id: 21093 reply_id: 84103[/import]