Can anybody explain to me how lines
[lua]ramp1.collision = Collision1
ramp1:addEventListener(“collision”,ramp1)[/lua]
work in the program. I wrote the program and everything works, I just want to know how those lines work. I used an example piece of code without really understanding it. I understand “collision” and Event listeners but I really cannot find anything to explain the ramp1.collision = Collision1 and how it ties in with the
function Collision1(self, event). What is “self”?
[lua]require ( “physics” )
physics.start()
hit = audio.loadSound(“hit.wav”)
local balls = {}
function BuildObstacles()
ramp1 = display.newRect(0,200,300,10)
ramp1:setFillColor(255,255,0)
ramp1.rotation = 30
ramp2 = display.newRect(200,400,300,10)
ramp2:setFillColor(255,0,0)
ramp2.rotation = -30
ramp3 = display.newRect(0,600,300,10)
ramp3:setFillColor(255,255,0)
ramp3.rotation = 30
leftWall = display.newRect(0,0,10,850)
leftWall:setFillColor(255,0,0)
rightWall = display.newRect(470,0,10,850)
rightWall:setFillColor(255,0,0)
bottom = display.newRect(0,846,470,10)
bottom:setFillColor(255,255,0)
end
function InitializeBall()
ball_blue = display.newImage( “ball_blue.png” )
ball_blue.x = 100
ball_blue.y = 0
physics.addBody( ball_blue, { bounce=0, radius = 15 } )
ball_red = display.newImage( “ball_red.png” )
ball_red.x = 200
ball_red.y = -40
physics.addBody( ball_red, { bounce=0, radius = 15 } )
ball_green = display.newImage( “ball_green.png” )
ball_green.x = 200
ball_green.y = -80
physics.addBody( ball_green, { bounce=0, radius = 15 } )
balls[1] = ball_blue
balls[2] = ball_red
balls[3] = ball_green
end
BuildObstacles()
InitializeBall()
–Function to check the ball’s location
local function CheckPos ()
for cnt = 1, 3 do
if balls[cnt].y > 830 then
balls[cnt]:setLinearVelocity(0,0)
balls[cnt].y = -40
balls[cnt].x = 40
end
end
end
function Collision1(self, event)
if event.phase==“began” then
audio.play(hit)
red = math.random(1,255)
blue = math.random(1,255)
green = math.random(1,255)
ramp1:setFillColor(red,blue,green)
end
end
function Collision2(self, event)
if event.phase==“began” then
audio.play(hit)
red = math.random(1,255)
blue = math.random(1,255)
green = math.random(1,255)
ramp2:setFillColor(red,blue,green)
end
end
function Collision3(self, event)
if event.phase==“began” then
audio.play(hit)
red = math.random(1,255)
blue = math.random(1,255)
green = math.random(1,255)
ramp3:setFillColor(red,blue,green)
end
end
ramp1.collision = Collision1
ramp1:addEventListener(“collision”,ramp1)
ramp2.collision = Collision2
ramp2:addEventListener(“collision”,ramp2)
ramp3.collision = Collision3
ramp3:addEventListener(“collision”,ramp3)
–Add a runtime listener
Runtime: addEventListener (“enterFrame”, CheckPos)
physics.addBody(bottom, “static”, {bounce = 0})
physics.addBody(rightWall, “static”)
physics.addBody(leftWall, “static”)
physics.addBody(ramp1, “static”, {bounce = 0})
physics.addBody(ramp2, “static”, {bounce = 0})
physics.addBody(ramp3, “static”, {bounce = 0})[/lua] [import]uid: 23256 topic_id: 24049 reply_id: 324049[/import]
