I need help with this onCollision function!

Hey im having big troubles making this onCollision function work good… here is what i need and have…

local arrow = {} local aCounter = 1 local timer1 box = display.newImageRect( "box.png", 50, 50 ) box.x = display.contentCenterX box.y = display.contentCenterY physics.addBody( box, "static", { bounce = 0, radius = 25 }) box.myName = "box" box.value = 1 local function spawnArrows() arrow[aCounter] = display.newImageRect( "Black.png", 30, 60 ) arrow[aCounter].x = display.contentCenterX arrow[aCounter].y = display.contentHeight \* -0.2 arrow[aCounter].rotation = 180 physics.addBody( arrow[aCounter], "dynamic", { bounce = 0 }) arrow[aCounter].value = aCounter arrow[aCounter].myName = "arrow" arrow[aCounter].gravityScale = 0 arrow[aCounter]:setLinearVelocity( 0, 200 ) aCounter = aCounter + 1 end timer1 = timer.performWithDelay( math.random(1000,2000), spawnArrows, -1 )

so the arrows move and need to hit the box and when they do they disappear… and that would be easy but i have 4 other functions like spawnArrows… like spawnArrows2 and so on… and i cant make a correct function fo the collision…

Thanks!

I tried something out but it doesnt seem to work…

local arrow = {} local aCounter = 1 local timer1 box = display.newImageRect( "box.png", 50, 50 ) box.x = display.contentCenterX box.y = display.contentCenterY physics.addBody( box, "static", { bounce = 0, radius = 25 }) box.myName = "box" box.value = 1 function onCollision( event) if event.phase == "began" then if event.myName == "Gem" and event.other.myName == "arrow" then print("hell0") end end end local function spawnArrows() arrow[aCounter] = display.newImageRect( "Black.png", 30, 60 ) arrow[aCounter].x = display.contentCenterX arrow[aCounter].y = display.contentHeight \* -0.2 arrow[aCounter].rotation = 180 physics.addBody( arrow[aCounter], "dynamic", { bounce = 0 }) arrow[aCounter].value = aCounter arrow[aCounter].myName = "arrow" arrow[aCounter].gravityScale = 0 arrow[aCounter]:setLinearVelocity( 0, 200 ) arrow[aCounter]:addEventListener( "collision", onCollision ) aCounter = aCounter + 1 end timer1 = timer.performWithDelay( math.random(1000,2000), spawnArrows, -1 )

But still doesnt seem to work…

Hi @SonicX278,

Try replacing this line in your code…

[lua]

arrow[aCounter]:addEventListener( “collision”, onCollision )

[/lua]

With these 2 lines…

[lua]

arrow[aCounter].collision = onCollision

arrow[aCounter]:addEventListener( “collision”, arrow[aCounter] )

[/lua]

Brent

i have both 

arrow[aCounter]:addEventListener( "collision", onCollision ) and arrow[aCounter].collision = onCollision arrow[aCounter]:addEventListener( "collision", arrow[aCounter])

and i always try out both listeners but it still doesnt seem to work.

You get absolutely no response from the listener whatsoever? Even if you put a print() statement in there as the first line (of the function), it never gets there?

If i do this 

function onCollision( event) if event.phase == "began" then print("hell0") end end arrow[aCounter]:addEventListener( "collision", onCollision )

it prints to console…

but this doesnt work…

function onCollision( event) if event.phase == "began" then if event.myName == "Gem" and event.other.myName == "arrow" then print("hell0") end end end

Check the second post to see how my code is set up.

There is no such thing as “event.myName”, so your conditional check fails.

More importantly though, you’re not passing the correct arguments to the function as indicated in the guide.

It should be this:

[lua]

local function onCollision( self, event )  – pass “self” + “event”, AND make this a local function (global is terrible practice)

[/lua]

Then, as stated in the guide, “self” is the object that collided, “event.other” is the other object. So it should be “event .other.myName”, not “event.myName”.

I tried something out but it doesnt seem to work…

local arrow = {} local aCounter = 1 local timer1 box = display.newImageRect( "box.png", 50, 50 ) box.x = display.contentCenterX box.y = display.contentCenterY physics.addBody( box, "static", { bounce = 0, radius = 25 }) box.myName = "box" box.value = 1 function onCollision( event) if event.phase == "began" then if event.myName == "Gem" and event.other.myName == "arrow" then print("hell0") end end end local function spawnArrows() arrow[aCounter] = display.newImageRect( "Black.png", 30, 60 ) arrow[aCounter].x = display.contentCenterX arrow[aCounter].y = display.contentHeight \* -0.2 arrow[aCounter].rotation = 180 physics.addBody( arrow[aCounter], "dynamic", { bounce = 0 }) arrow[aCounter].value = aCounter arrow[aCounter].myName = "arrow" arrow[aCounter].gravityScale = 0 arrow[aCounter]:setLinearVelocity( 0, 200 ) arrow[aCounter]:addEventListener( "collision", onCollision ) aCounter = aCounter + 1 end timer1 = timer.performWithDelay( math.random(1000,2000), spawnArrows, -1 )

But still doesnt seem to work…

Hi @SonicX278,

Try replacing this line in your code…

[lua]

arrow[aCounter]:addEventListener( “collision”, onCollision )

[/lua]

With these 2 lines…

[lua]

arrow[aCounter].collision = onCollision

arrow[aCounter]:addEventListener( “collision”, arrow[aCounter] )

[/lua]

Brent

i have both 

arrow[aCounter]:addEventListener( "collision", onCollision ) and arrow[aCounter].collision = onCollision arrow[aCounter]:addEventListener( "collision", arrow[aCounter])

and i always try out both listeners but it still doesnt seem to work.

You get absolutely no response from the listener whatsoever? Even if you put a print() statement in there as the first line (of the function), it never gets there?

If i do this 

function onCollision( event) if event.phase == "began" then print("hell0") end end arrow[aCounter]:addEventListener( "collision", onCollision )

it prints to console…

but this doesnt work…

function onCollision( event) if event.phase == "began" then if event.myName == "Gem" and event.other.myName == "arrow" then print("hell0") end end end

Check the second post to see how my code is set up.

There is no such thing as “event.myName”, so your conditional check fails.

More importantly though, you’re not passing the correct arguments to the function as indicated in the guide.

It should be this:

[lua]

local function onCollision( self, event )  – pass “self” + “event”, AND make this a local function (global is terrible practice)

[/lua]

Then, as stated in the guide, “self” is the object that collided, “event.other” is the other object. So it should be “event .other.myName”, not “event.myName”.