[Resolved] Collision: attempt to index field 'object1' (a nil value)

Hi everybody,

Just started to play with Corona for a while, looks very promising indeed, but I hit a major obstacle.

I just could not manage to get the collision event to report either object1 or object2, here are the codes I am struggling with:

_display.setStatusBar(display.HiddenStatusBar)

local background = display.newImage(“background.png”)

local obj1 = display.newImage(“object1.png”)
obj1.myName = “ball”
physics = require(“physics”);
physics.start()
physics.setGravity( 0, 0 )

physics.addBody(obj1, “dynamic”, {radius=25})
local leftWall = display.newRect(0, 0, 1, display.contentHeight)
leftWall:setFillColor(192, 255, 255)
leftWall.myName = “leftWall”
physics.addBody( leftWall, “static”)

local rightWall = display.newRect(display.contentWidth-1, 0, 10, display.contentHeight)
rightWall:setFillColor(192, 255, 255)
rightWall.myName = “rightWall”
physics.addBody( rightWall, “static”)

local bottomWall = display.newRect(0, display.contentHeight-1, display.contentWidth, 1)
bottomWall:setFillColor(192, 255, 255)
bottomWall.myName = “bottomWall”
physics.addBody( bottomWall, “static”)

local topWall = display.newRect(0, 0, display.contentWidth, 1)
topWall:setFillColor(192, 255, 255)
topWall.myName = “topWall”
physics.addBody( topWall, “static”)

local function bounceFromWall( event )

print( "event: " …event.name… "; object1: " …event.object1.myName… "; object2: " …event.object2.myName )

end

obj1:addEventListener(“collision”, bounceFromWall)
leftWall:addEventListener(“collision”, bounceFromWall)
rightWall:addEventListener(“collision”, bounceFromWall)
topWall:addEventListener(“collision”, bounceFromWall)
bottomWall:addEventListener(“collision”, bounceFromWall)

function moveRandomly()

obj1:setLinearVelocity( 100, 150);

end

timer.performWithDelay(500, moveRandomly, 1);_

It seems the bounceFromWall function could be triggered, but it just kept telling me “attempt to index field ‘object1’ (a nil value)”. Not just object1, but also object2, were not exposed from the ‘event’ object variable at all.

Anybody have ideas about that?
Jimmy [import]uid: 11908 topic_id: 33324 reply_id: 333324[/import]

print( "event: " …event.name… "; object1: " …event.object1.myName… "; object2: " …event.object2.myName )
event.object1.myName
event.obj1.myName

Declare obj2. [import]uid: 116842 topic_id: 33324 reply_id: 132332[/import]

Hi Jimmy,
Are you using a global (Runtime) collision listener, or a local (object-based) collision listener? The setup is somewhat distinct for each usage and must be adhered to carefully. Also, only the global method exposes “object1” and “object2”; the local version uses “self” and “event.other” instead.

Read the examples here very carefully:
http://developer.coronalabs.com/content/game-edition-collision-detection

As a side note, in your example with an object bouncing off walls, there shouldn’t be a need to add listeners to everything. Only the “object” needs one… using that, you can just read the “other” (the wall) during a collision, if you need to perform some action on the wall. It doesn’t need its own listener, as that would be redundant and not exactly performance-friendly. :stuck_out_tongue:

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 33324 reply_id: 132383[/import]

Hi Brent,

It works now! Thanks buddy!

Nevertheless the implementation difference between the Global and Local Listener, also the discrepancy of the event procedure styles, really confuses me… >_Jimmy [import]uid: 11908 topic_id: 33324 reply_id: 132405[/import]

print( "event: " …event.name… "; object1: " …event.object1.myName… "; object2: " …event.object2.myName )
event.object1.myName
event.obj1.myName

Declare obj2. [import]uid: 116842 topic_id: 33324 reply_id: 132332[/import]

Hi Jimmy,
Are you using a global (Runtime) collision listener, or a local (object-based) collision listener? The setup is somewhat distinct for each usage and must be adhered to carefully. Also, only the global method exposes “object1” and “object2”; the local version uses “self” and “event.other” instead.

Read the examples here very carefully:
http://developer.coronalabs.com/content/game-edition-collision-detection

As a side note, in your example with an object bouncing off walls, there shouldn’t be a need to add listeners to everything. Only the “object” needs one… using that, you can just read the “other” (the wall) during a collision, if you need to perform some action on the wall. It doesn’t need its own listener, as that would be redundant and not exactly performance-friendly. :stuck_out_tongue:

Best regards,
Brent Sorrentino [import]uid: 9747 topic_id: 33324 reply_id: 132383[/import]

Hi Brent,

It works now! Thanks buddy!

Nevertheless the implementation difference between the Global and Local Listener, also the discrepancy of the event procedure styles, really confuses me… >_Jimmy [import]uid: 11908 topic_id: 33324 reply_id: 132405[/import]