addEventListener isnt working..

Hey guys! so ive ran into a problem that cant seem to figure out… so when i start the game it gives me an error saying…

Runtime error ?:0: attempt to call method 'respondsToEvent' (a nil value) stack traceback: ?: in function 'addEventListener' c:\users\ruvim\documents\main game\faller\game.lua:80: in function '\_lis tener' ?: in function \<?:141\> ?: in function \<?:221\>

and heres the code and where its giving the error

 local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle].addEventListener( "collision", circle[bCircle] ) \<------ line 80!! circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 )

SideNote - @roaminggamer - if you end up helping me hope the code i posted is more clear and easy to read :slight_smile: if not please tell me what to fix…

SideNote2 - when i comment out line 80 everything works perfect except the collision that i want doesnt happen…

thanks for any help :slight_smile:

Hi @SonicX278,

The code you’ve posted doesn’t show how “respondsToEvent” relates to anything else (nor do you show that anywhere, or whether it’s a function or not). Your error seems to be there, so unless we can see how that function is getting called, it’s difficult to provide any assistance.

Take care,

Brent

Well the spawnCirlces function is being called to spawn randoms cirlces and make them fall down and then i have a box at the bottom and when a circle collides with the box the onCollision function is suposed to be called and print something in the console… heres the code…

 local circle = {} local bCircle = 1 local circleTimer local box = display.newImageRect("box.png", 30, 30) physics.addBody(box, "static", {bounce = 0} ) box.x = display.contentWidth \* 0.5 box.y = 415 box.value = 1 local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then display.remove(circle[hit]) circle[hit] = nil print("hellooo") else display.remove(circle[hit]) circle[hit] = nil end end end local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle].addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 )

thats all the code that you need to see whats wrong… 

thanks :slight_smile:

Hi.

You want to use colon syntax:

circle[bCircle] : addEventListener( "collision", circle[bCircle] ) \<------ line 80!! -- ^^ THIS

or, equivalently (and much more confusingly!):

circle[bCircle].addEventListener(circle[bCircle], "collision", circle[bCircle] ) \<------ line 80!!

As is, you’re trying to call an addEventListener method belonging to the string “collision”.

EDIT : Sorry, that was poorly worded. You are trying to call a method belonging to the circle, but since the dot syntax doesn’t give it the hint to pass itself as the first argument, the method sees the string there instead, and thinks it belongs to that.

Hi @SonicX278,

The code you’ve posted doesn’t show how “respondsToEvent” relates to anything else (nor do you show that anywhere, or whether it’s a function or not). Your error seems to be there, so unless we can see how that function is getting called, it’s difficult to provide any assistance.

Take care,

Brent

Well the spawnCirlces function is being called to spawn randoms cirlces and make them fall down and then i have a box at the bottom and when a circle collides with the box the onCollision function is suposed to be called and print something in the console… heres the code…

 local circle = {} local bCircle = 1 local circleTimer local box = display.newImageRect("box.png", 30, 30) physics.addBody(box, "static", {bounce = 0} ) box.x = display.contentWidth \* 0.5 box.y = 415 box.value = 1 local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value if other == 1 then display.remove(circle[hit]) circle[hit] = nil print("hellooo") else display.remove(circle[hit]) circle[hit] = nil end end end local spawnCircles = function() local Fall = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8) circle[bCircle] = display.newImageRect("circle.png", 31, 31 ) circle[bCircle].x = Fall circle[bCircle].y = -70 physics.addBody( circle[bCircle], "dynamic", {bounce = 0} ) physics.setGravity( 0, 0.5) circle[bCircle].collision = onCollision circle[bCircle].addEventListener( "collision", circle[bCircle] ) circle[bCircle].value = bCircle bCircle = bCircle + 1 end box.collision = onCollision circleTimer = timer.performWithDelay( 1000, spawnCircles, -1 )

thats all the code that you need to see whats wrong… 

thanks :slight_smile:

Hi.

You want to use colon syntax:

circle[bCircle] : addEventListener( "collision", circle[bCircle] ) \<------ line 80!! -- ^^ THIS

or, equivalently (and much more confusingly!):

circle[bCircle].addEventListener(circle[bCircle], "collision", circle[bCircle] ) \<------ line 80!!

As is, you’re trying to call an addEventListener method belonging to the string “collision”.

EDIT : Sorry, that was poorly worded. You are trying to call a method belonging to the circle, but since the dot syntax doesn’t give it the hint to pass itself as the first argument, the method sees the string there instead, and thinks it belongs to that.