Physics and Global Collision

Hi

Just starting out with Corona but love it so far:) However I have hit a bit of a snag that I can’t get my head around and wondered if some would be kind enough to cast some light on it.

I am creating a kind of grapple arm so it shoots out from my hero and if it hits say a wall or platform it will act as a grapple would:)

I am currently trying to work out collision detection and have hit a snag. Here is the relevant part of my code:


function createBeam()

circle = display.newCircle(50, 50, 10)
circle:setFillColor(0,255,00)
circle.x = hero.x
circle.y = hero.y
localGroup:insert(circle)

angle = hero:getAngleTo(touchX, touchY)

physics.addBody(circle, “kinematic”, {bounce = 0})
circle.myName = “grapple”
–circle.isBullet = true
circle.collision = onCollision
transition.to(circle, {time = 1000, y = touchY, x=touchX})

line = display.newLine(hero.x, hero.y, circle.x, circle.y)
localGroup:insert(line)
Runtime:addEventListener( “enterFrame”, animateBeam )
Runtime:addEventListener( “collision”, onCollision )

end
function animateBeam()

localGroup:remove( line )
line = display.newLine(hero.x, hero.y, circle.x, circle.y)
localGroup:insert(line)

end

local function onCollision( event )
if ( event.phase == “began” ) then

print( "began: " … event.object1.myName … " & " … event.object2.myName )

elseif ( event.phase == “ended” ) then

print( "ended: " … event.object1.myName … " & " … event.object2.myName )

end
end
function addTarget( event )
if event.phase == “began” then
touchX = event.x
touchY = event.y

createBeam()

end
if event.phase == “ended” then
print(“touch has ended”)
localGroup:remove(circle)
localGroup:remove(line)
Runtime:removeEventListener( “enterFrame”, animateBeam )
Runtime:removeEventListener( “collision”, onCollision )

end
end

–> Add listener to the stage for above method.
Runtime:addEventListener(“touch”, addTarget)


Since I have added the collision elements I get this runtime error in the terminal:


Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’
/Users/adriangoddard/Desktop/build/main.lua:93: in function ‘createBeam’
/Users/adriangoddard/Desktop/build/main.lua:144: in function
?: in function <?:214>
ended

Has anyone any ideas why this would happen? The collision code is straight out of the reference guide and I have tested the ref guide code in full in a separate build. And it works fine. So there is obviously something amiss with elements are my code…but no idea what it could be:|

Any pointers would be really appreciated

ade
[import]uid: 66324 topic_id: 11043 reply_id: 311043[/import]

I think your problem is:

Runtime:addEventListener( "collision", onCollision )  

the function onCollision doesn’t exist when you call it b/c you declare it later down.

Here are two ways to fix this:

  1. Move the onCollision method to the top
  2. Declare local onCollision above the createBeam method and define it later like onCollision = function(event) [import]uid: 49205 topic_id: 11043 reply_id: 40217[/import]

awinograd’s advice sounds about right, without testing, obviously :wink:

In future please post your code within lua tags; it’s a headache to read otherwise. Thanks!

Peach :slight_smile: [import]uid: 52491 topic_id: 11043 reply_id: 40236[/import]

Thanks yes that fixed it:) Slaps head

And Yes Peach will post code better next time…it had been a long day:)

thanks [import]uid: 66324 topic_id: 11043 reply_id: 40268[/import]

Glad I could help! [import]uid: 49205 topic_id: 11043 reply_id: 40274[/import]