addEventListener

How do I add an event listener for an object from an array?
I wan’t a collision event to happen for the object.

[lua]local function spawnCoinz()

local coine = {}
for i = 1, 40, 1 do
coinss = display.newImage(“Coin2.png”)
coine[i] = coinss
coinss.x = 150
coinss.y = 100

end

physics.addBody( coine[1], { density = 3.0; friction=0.5; bounce = 0.3 } )
physics.addBody( coine[2], { density = 3.0; friction=0.5; bounce = 0.3 } )

end
coine[1].collision = onLocalCollision
coine[1]:addEventListener( “collision”, coine )
–Debugger says coine is a nil value
–How do I make it so I can access the object of the array and create an event listener for it? Thanks. [import]uid: 54001 topic_id: 9597 reply_id: 309597[/import]

I’m having the same problem, anyone has a solution for this?

[import]uid: 50459 topic_id: 9597 reply_id: 37026[/import]

You put this:

local coine = {}

…inside a function and then are trying to access it outside of that function.

Do:

local coine

…outside the function and then:

coine = {}

…inside the function.

Jay

PS - Or, if you don’t want to use a global variable like that, leave local coine = {} the way it is but call the function like this:

local coineArr = spawnCoinz()

…and have the function do:

return coine

…at the end of it. Now you can do stuff like:

coineArr[1].collision = onLocalCollision

[import]uid: 9440 topic_id: 9597 reply_id: 37032[/import]

coine[1].collision = onLocalCollision

is not the problem, the following line is:

coine[1]:addEventListener( “collision”, coine )

Debugger says coine is a nil value

[import]uid: 50459 topic_id: 9597 reply_id: 37182[/import]

Yes, I’m pretty sure my post fixed that problem – although I’ll agree that I didn’t go into detail. :slight_smile:

I figured the snippet posted was just parts, so here’s an entire program that works (I changed coin pic to star pic since that’s what I had handy):

local physics = require("physics")  
physics.start()  
  
local coine = {}  
  
local function spawnCoinz()  
 coine = {}  
 for i = 1, 40, 1 do  
 coinss = display.newImage("star.png")  
 coine[i] = coinss  
 coinss.x = 150  
 coinss.y = 100  
 end  
 physics.addBody( coine[1], { density = 3.0; friction=0.5; bounce = 0.3 } )  
 physics.addBody( coine[2], { density = 3.0; friction=0.5; bounce = 0.3 } )  
end  
   
spawnCoinz()  
  
coine[1].collision = onLocalCollision  
coine[1]:addEventListener( "collision", coine )  

coine[1].collision = onLocalCollision has a problem because spawnCoinz() wasn’t called yet. In the code above I called it before trying to use that table.

So define the coine table outside of the spawnCoinz function and then call spawnCoinz before you try and use the values of the coine table.

That chunk of code above works and spawns two stars that fall down the screen.

By moving the physics.addBody line inside the loop and changing it to this:

physics.addBody( coine[i], { density = 3.0; friction=0.5; bounce = 0.3 } )

…it spawned 40 stars that fell down the screen.

I hope that helps.

Jay
[import]uid: 9440 topic_id: 9597 reply_id: 37213[/import]

And a quick follow-up – not sure if you *want* to add the collision stuff afterwards for a specific reason, but you could fold it all into the loop, like this:

local function spawnCoinz()  
 coine = {}  
 for i = 1, 4, 1 do  
 coinss = display.newImage("star.png")  
 coine[i] = coinss  
 coinss.x = 150  
 coinss.y = 100  
 physics.addBody( coine[i], { density = 3.0; friction=0.5; bounce = 0.3 } )  
 coine[i].collision = onLocalCollision  
 coine[i]:addEventListener( "collision", coine )  
 end  
end  

Jay
[import]uid: 9440 topic_id: 9597 reply_id: 37214[/import]

Thanks a lot, i got it working now! [import]uid: 50459 topic_id: 9597 reply_id: 37298[/import]