How does this work "ramp1.collision = Collision1"

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]

The answers are right there in front of you.
look at line 87, then look at your functions. Do you see something that they have in common?

line 88 you have a listener on ramp1 object, it’s a collision.

Also notice that there is a shortcut, line 87 you are saying ramp1.collision = Collision1

Sooooooooooo, look at line 88 again and what is after “collision”?
Then look at line 59. Then look at line 87. Do you see anything there? Ramp1.collision, also has another name…
I just tested it out, so I do know. So rather than tell you, this will teach you something :slight_smile:

The answer is there.

:slight_smile:

ng
Unless I am completely off base here, I think you will have this figured out pretty quick :slight_smile:

[import]uid: 61600 topic_id: 24049 reply_id: 97019[/import]

I have about 90% sort of figured out. I am just not sure what the ramp1.collision = Collision1 is doing. I realize it is giving ramp1.collision a new name but is the .collision somehow connected to the “collision” in the event listener? I thought the .collision is a user made property while the “collision” in the event listener is a system recognized word. I am also unclear as to what job the self and event are doing in the parameter list in the function. I can cut and paste and make things work. I am just trying to get to the next level of understanding. Thanks for the help. [import]uid: 23256 topic_id: 24049 reply_id: 97131[/import]

It is creating a new function called collision for the ramp1 object that is equal to the Collision1 function.

Mark [import]uid: 117098 topic_id: 24049 reply_id: 97538[/import]

Mark, That actually makes sense. Any idea about the “self” in the function list? [import]uid: 23256 topic_id: 24049 reply_id: 97822[/import]

Self is the object itself so in ramp1.collision, the self would be the ramp1 object.

Mark [import]uid: 117098 topic_id: 24049 reply_id: 98339[/import]

Thank Mark. In a very piecewise manner this is starting to come together. [import]uid: 23256 topic_id: 24049 reply_id: 98602[/import]