Corona physics collision runtime error - Pls help tried everything

Hi Guys,

Getting stuck in a code I am playing around with. I am getting run time scene_game.lua:290: attempt to call method ‘removeEventListener’ (a nil value) error. 290 is referring to this part of the code           

   for i=1,#lanes do                     lanes[i]:removeEventListener("touch", moveCar)                 end

The funny part is this error does not get invoked when the collision in

   

 elseif (self=="enemy" and other=="player") then

Rather, it only gets invoked when this part of the code shown below gets executed.

  

  elseif (self=="player" and other=="enemy") then

The whole section of the code is provided below for Your reference. Pls help!

   

 function scene:create( event ) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Initialize the scene here. &nbsp; &nbsp; &nbsp; &nbsp; -- Example: add display objects to "sceneGroup", add touch listeners, etc. &nbsp; &nbsp; &nbsp; &nbsp; local sceneGroup = self.view &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- The following variables are known as forward declares. We'll create some for the player, enemy, lanes and establish default values. &nbsp; &nbsp; &nbsp; &nbsp; local lanes = {} -- create a table called lanes &nbsp; &nbsp; &nbsp; &nbsp; local playerCar -- a variable for the player car &nbsp; &nbsp; &nbsp; &nbsp; local playerCar1 -- a variable for the player car &nbsp; &nbsp; local sprite &nbsp; &nbsp; &nbsp; &nbsp; local laneID = 1 -- a variable for the land id &nbsp; &nbsp; &nbsp; &nbsp; local enemyCars = {} -- a table to hold the enemy cars &nbsp; &nbsp; &nbsp; &nbsp; local enemyCounter = 1 -- start the enemy counter at 1 to keep track of the enemy cars &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local sendEnemyFrequency = 2500 -- defines how often to send enemy cars &nbsp; &nbsp; &nbsp; &nbsp; local tmrToSendCars -- a variable to hold a reference to the timer of sending cars &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local playerScore = 0 -- start the player score at 0 &nbsp; &nbsp; &nbsp; &nbsp; local playerScoreText -- an object to hold the score text object &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- This function will increment the player score by 1. This function is called when the transition for the enemy car is complete and is off screen. &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local sheetInfo = require("zo") &nbsp; &nbsp; local myImageSheet = graphics.newImageSheet( "zo.png", sheetInfo:getSheet() ) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local sequenceData = { &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; name="walk", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- name of the animation &nbsp; &nbsp; &nbsp; &nbsp; sheet=myImageSheet, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- the image sheet &nbsp; &nbsp; &nbsp; &nbsp; start=sheetInfo:getFrameIndex("1"), -- first frame &nbsp; &nbsp; &nbsp; &nbsp; count=4, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- number of frames &nbsp; &nbsp; &nbsp; &nbsp; time=700, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- speed &nbsp; &nbsp; &nbsp; &nbsp; loopCount=0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- repeat &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local sheetInfo2 = require("opo") &nbsp; &nbsp; local myImageSheet2 = graphics.newImageSheet( "opo.png", sheetInfo2:getSheet() ) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local sequenceData2 = { &nbsp; &nbsp; &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; &nbsp; name="walk2", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- name of the animation &nbsp; &nbsp; &nbsp; &nbsp; sheet=myImageSheet, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- the image sheet &nbsp; &nbsp; &nbsp; &nbsp; start=sheetInfo:getFrameIndex("1"), -- first frame &nbsp; &nbsp; &nbsp; &nbsp; count=4, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- number of frames &nbsp; &nbsp; &nbsp; &nbsp; time=700, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- speed &nbsp; &nbsp; &nbsp; &nbsp; loopCount=0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- repeat &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chk=0 &nbsp; &nbsp; function messenger2(text) &nbsp; &nbsp; if chk==0 then &nbsp; &nbsp; g4A = display.newGroup() &nbsp; &nbsp; halt1= display.newRect( -30 , 150, 50+100\*9, 44\*2+30-65 ) &nbsp; &nbsp; halt1.alpha=.85 &nbsp; &nbsp; halt1.anchorX=0 &nbsp; &nbsp; halt1.anchorY=0 &nbsp; &nbsp; halt1:setFillColor( .2, .2, .2 ,.95) &nbsp; &nbsp; halt1:setStrokeColor( .8, .8 ,.8 ) &nbsp; &nbsp; halt1.strokeWidth = .51 &nbsp; &nbsp; audio.play(levelup) &nbsp; &nbsp; halt1text = display.newText( text, 235, 44\*3+185-140,native.systemFont, 26 ) &nbsp; &nbsp; halt1text:setFillColor( .8, .8, .8 ) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;g4A:insert( halt1 )&nbsp; &nbsp; &nbsp; &nbsp; g4A:insert( halt1text ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;transition.fadeOut( g4A, { time=2500, transition=easing.inCirc &nbsp;} ) &nbsp; &nbsp; &nbsp; &nbsp; sceneGroup:insert(g4A)&nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local function incrementScore() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScore = playerScore + 1 -- add playerScore by 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScoreText.text = "Score: "..playerScore -- update the on screen text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if playerScore\>bestscore then &nbsp; &nbsp; bestscore=playerScore &nbsp; &nbsp; &nbsp; &nbsp; messenger2("New high score reached") &nbsp; &nbsp; chk=1 &nbsp; &nbsp; loadsave.saveTable(bestscore, "Bestscorenow.json", system.DocumentsDirectory) &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- moveCar will respond to the touch event on the lanes &nbsp; &nbsp; &nbsp; &nbsp; local function moveCar(event) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(event.phase == "ended") then&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; laneID = event.target.id -- grab the lane id which will be 1, 2, or 3 &nbsp; &nbsp; &nbsp; &nbsp; transition.to(playerCar, {x=lanes[laneID].x,time=50}) -- move the player car to the appropriate lane &nbsp; &nbsp; &nbsp; &nbsp; transition.to(playerCar1, {x=lanes[laneID].x,time=50}) -- move the player car to the appropriate lane &nbsp; &nbsp; &nbsp; &nbsp; transition.to(sprite, {x=lanes[laneID].x,time=50}) -- move the player car to the appropriate lane &nbsp; &nbsp; &nbsp; &nbsp; audio.play(movingck) &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true -- to indicate a successful touch event, return true &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- sendEnemyCar is where the magic happens. This function will send enemy cars from the top of the screen to the bottom of the screen. &nbsp; &nbsp; &nbsp; &nbsp; local function sendEnemyCar() &nbsp; &nbsp; if(enemyCounter%7 == 0) then &nbsp; &nbsp; enemyCars[enemyCounter] = display.newImageRect(sceneGroup, "tr.png", 20, 20) &nbsp; &nbsp; enemyCars[enemyCounter].x = lanes[math.random(1,#lanes)].x -- place the car on a random lane &nbsp; &nbsp; &nbsp; &nbsp; if(math.random(1,2) == 1) then enemyCars[enemyCounter].x = lanes[laneID].x; end -- 50% of the time, place the enemy car on the player car lane.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].y = -125 -- place the enemy off screen at the top &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter]:scale(1.8,-1.65) -- rotate the cars so they are facing down &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.addBody(enemyCars[enemyCounter]) -- add a physics body to enemy cars &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].bodyType = "kinematic" -- make the bodies kinematic &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].id = "trophy" -- id &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(enemyCars[enemyCounter], {y=display.contentHeight+enemyCars[enemyCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); incrementScore(); end}) -- a transition that moves the enemy car towards the bottom of the screen. On completion, the enemy car object is removed from the game. &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemyCounter = enemyCounter + 1 -- increase enemy counter by one for tracking &nbsp; &nbsp; &nbsp; &nbsp; if(enemyCounter%2 == 0) then -- every other car, increase the speed of enemy frequency &nbsp; &nbsp; &nbsp; &nbsp; sendEnemyFrequency = sendEnemyFrequency - 200 -- deduct the frequency by 200ms &nbsp; &nbsp; &nbsp; &nbsp; if(sendEnemyFrequency \< 850) then sendEnemyFrequency = 850; end -- cap the send enemy frequency to 800 &nbsp; &nbsp; &nbsp; &nbsp; timer.cancel(tmrToSendCars) -- cancel the timer of sending cars &nbsp; &nbsp; &nbsp; &nbsp; tmrToSendCars = timer.performWithDelay(sendEnemyFrequency, sendEnemyCar, 0) -- recreate the time to send cars with update frequency &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter] = display.newSprite(sceneGroup, myImageSheet2, sequenceData2 ) &nbsp; &nbsp; enemyCars[enemyCounter]:setSequence("walk2") &nbsp; &nbsp; enemyCars[enemyCounter]:play() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].x = lanes[math.random(1,#lanes)].x -- place the car on a random lane &nbsp; &nbsp; &nbsp; &nbsp; if(math.random(1,2) == 1) then enemyCars[enemyCounter].x = lanes[laneID].x; end -- 50% of the time, place the enemy car on the player car lane.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].y = -125 -- place the enemy off screen at the top &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter]:scale(1.8,-1.65) -- rotate the cars so they are facing down &nbsp; &nbsp; local offsetRectParams = { halfWidth=20, halfHeight=20, x=0, y=60, angle=0 } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.addBody(enemyCars[enemyCounter], { box=offsetRectParams } ) -- add a physics body to enemy cars &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].bodyType = "kinematic" -- make the bodies kinematic &nbsp; &nbsp; &nbsp; &nbsp; enemyCars[enemyCounter].id = "enemy" -- id &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(enemyCars[enemyCounter], {y=display.contentHeight+enemyCars[enemyCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); incrementScore(); end}) -- a transition that moves the enemy car towards the bottom of the screen. On completion, the enemy car object is removed from the game. &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemyCounter = enemyCounter + 1 -- increase enemy counter by one for tracking &nbsp; &nbsp; &nbsp; &nbsp; if(enemyCounter%2 == 0) then -- every other car, increase the speed of enemy frequency &nbsp; &nbsp; &nbsp; &nbsp; sendEnemyFrequency = sendEnemyFrequency - 200 -- deduct the frequency by 200ms &nbsp; &nbsp; &nbsp; &nbsp; if(sendEnemyFrequency \< 850) then sendEnemyFrequency = 850; end -- cap the send enemy frequency to 800 &nbsp; &nbsp; &nbsp; &nbsp; timer.cancel(tmrToSendCars) -- cancel the timer of sending cars &nbsp; &nbsp; &nbsp; &nbsp; tmrToSendCars = timer.performWithDelay(sendEnemyFrequency, sendEnemyCar, 0) -- recreate the time to send cars with update frequency &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Allow the player to return to the menu &nbsp; &nbsp; &nbsp; &nbsp; local function onPlayAgainTouch() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; composer.gotoScene("scene\_menu", "fade") -- move player to menu &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; local function playersdead() &nbsp; &nbsp; &nbsp; &nbsp; audio.play(deads) &nbsp; &nbsp; print(deads) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- stop the game &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.pause() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer.cancel(tmrToSendCars) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.pause() &nbsp; &nbsp; &nbsp;playerCar1.isVisible = true &nbsp; &nbsp; &nbsp;playerCar.alpha = 0 &nbsp; &nbsp; &nbsp;sprite.alpha = 0 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- remove event listeners from all lanes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i=1,#lanes do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lanes[i]:removeEventListener("touch", moveCar) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; Runtime:removeEventListener( "collision",onGlobalCollision ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local gameOverBackground = display.newRect(sceneGroup, 0, 0, display.actualContentWidth, display.actualContentHeight) -- display an opaque background graphic for some game over polish &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverBackground.x = display.contentCenterX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverBackground.y = display.contentCenterY &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverBackground:setFillColor(0) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverBackground.alpha = 0.5 &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- Create a text object that will display game over text &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local gameOverText = display.newText( sceneGroup, "Game Over!", 100, 200, native.systemFontBold, 36 ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverText.x = display.contentCenterX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverText.y = 150 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOverText:setFillColor( 1, 1, 1 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;wpmaGlobalAdmob.showAdmobInterstitialAd()&nbsp; &nbsp; &nbsp; wpmaGlobalAdmob.loadAdmobInterstitialAd() &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- create a button that allows the player to return to the &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local playAgain = widget.newButton { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = 220, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = 100, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defaultFile = "images/btn-blank.png", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overFile = "images/btn-blank.png", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label = "Menu", &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font = system.defaultFontBold, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontSize = 32, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } }, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onEvent = onPlayAgainTouch &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playAgain.x = display.contentCenterX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playAgain.y = gameOverText.y + 100 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sceneGroup:insert(playAgain) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; local function trophyz() &nbsp; &nbsp; &nbsp;audio.play(uplevel) &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -- This is the global collision scene. There are several ways to handle collisions and this is only one method. I felt this was the easiest for learning purposes. &nbsp; &nbsp; &nbsp; &nbsp; local function onGlobalCollision(event) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(event.phase == "began") then -- when the enemy car collides into the player car, this if/then statement will be true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local self = event.object2.id &nbsp; &nbsp; local other = event.object1.id &nbsp; &nbsp; &nbsp; &nbsp; print\_r( self) &nbsp; &nbsp; &nbsp; --the first object in the collision &nbsp; &nbsp; &nbsp; print\_r( other ) &nbsp; &nbsp; &nbsp; --the second object in the collision &nbsp; &nbsp; &nbsp;-- &nbsp; print\_r( event.element1 ) &nbsp; &nbsp; &nbsp;--the element (number) of the first object which was hit in the collision &nbsp; &nbsp; &nbsp;-- &nbsp; print\_r( event.element2 ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (self=="player" and other=="trophy") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;event.object1:removeSelf() &nbsp; &nbsp; trophyz() &nbsp; &nbsp; &nbsp;elseif (self=="trophy" and other=="player") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.object2:removeSelf() &nbsp; &nbsp; &nbsp;trophyz() &nbsp; &nbsp; &nbsp;elseif (self=="player" and other=="enemy") then &nbsp; &nbsp; &nbsp; playersdead() &nbsp; &nbsp; &nbsp;print("Someting screwy here") &nbsp; &nbsp; &nbsp;elseif (self=="enemy" and other=="player") then &nbsp; &nbsp; &nbsp; playersdead() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; --display.remove(self) &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp;print("U SHOULD NOT BE HERE") &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--END COLLISION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local background = display.newImageRect(sceneGroup, "images/background.png", 475, 713) -- create the background image object &nbsp; &nbsp; &nbsp; &nbsp; background.x = display.contentCenterX -- place the graphic in the center of the x-axis &nbsp; &nbsp; &nbsp; &nbsp; background.y = display.contentCenterY -- place the graphic in the center of the y-axis &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i=1,3 do -- loop 3 times to create 3 lanes for our game &nbsp; &nbsp; &nbsp; &nbsp;lanes[i] = display.newImageRect(sceneGroup, "images/lane.png", 79, 713) &nbsp; &nbsp; &nbsp; &nbsp; lanes[i].x = (display.contentCenterX - 79\*2) + (i\*80) &nbsp; &nbsp; &nbsp; &nbsp; lanes[i].y = display.contentCenterY &nbsp; &nbsp; &nbsp; &nbsp; lanes[i].id = i &nbsp; &nbsp; &nbsp; &nbsp; lanes[i]:addEventListener("touch", moveCar) -- add an event listener to the lanes that will respond to touch events. &nbsp; &nbsp; end &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScoreText = display.newText(sceneGroup, "Score: "..playerScore, 0, 0, native.systemFont, 36) -- Create a text object that will display the player score &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScoreText.x = display.contentCenterX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerScoreText.y = 25 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; playerCar = display.newImageRect(sceneGroup, "images/playerCar.png", 60, 60) -- create the player car image object &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerCar.anchorY = 1 -- set the anchor point to 1 which is the bottom of the graphic &nbsp; &nbsp; &nbsp; playerCar.x = lanes[1].x -- put the player car on the first lane &nbsp; &nbsp; &nbsp; playerCar.y = display.contentHeight-70 -- place the car at the bottom of the screen &nbsp; &nbsp; &nbsp; physics.addBody(playerCar) -- add a physics body to the car &nbsp; &nbsp; &nbsp; playerCar.bodyType = "dynamic" -- make the car a dynamic body type &nbsp; &nbsp; playerCar.id = "player"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerCar1 = display.newImageRect(sceneGroup, "images/playerCar11.png", 50, 100) -- create the player car image object &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerCar1.anchorY = 1 -- set the anchor point to 1 which is the bottom of the graphic &nbsp; &nbsp; &nbsp; playerCar1.x = lanes[1].x -- put the player car on the first lane &nbsp; &nbsp; &nbsp; playerCar1.y = display.contentHeight -- place the car at the bottom of the screen &nbsp; &nbsp; -- id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- physics.addBody(playerCar1) -- add a physics body to the car &nbsp; &nbsp; &nbsp;-- playerCar1.bodyType = "dynamic" -- make the car a dynamic body type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sprite = display.newSprite( myImageSheet, sequenceData ) &nbsp; &nbsp; sprite:scale( 1.6, 1.6) &nbsp; &nbsp; sprite.anchorY = 1 &nbsp; &nbsp; sprite:setSequence("walk") &nbsp; &nbsp; sprite:play() &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; sprite.x = lanes[1].x -- put the player car on the first lane &nbsp; &nbsp; &nbsp; sprite.y = display.contentHeight -- place the car at the bottom of the screen &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;playerCar1.isVisible = false &nbsp; &nbsp; &nbsp;playerCar.isVisible = true &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;playerCar.alpha = 0 &nbsp; &nbsp; &nbsp; &nbsp; tmrToSendCars = timer.performWithDelay(sendEnemyFrequency, sendEnemyCar, 0) -- start a timer to send cards. A 0 means run forever &nbsp; &nbsp; &nbsp; &nbsp; Runtime:addEventListener( "collision", onGlobalCollision ) -- create a global event listener to listen for any collision. &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; end

attempt to call method ‘removeEventListener’ (a nil value) = your object is already removed.

Always check an object is valid before you try and do anything to it.  Like this

if myObject then &nbsp;--do something with myObject end

FYI, It is always best practice to iterate loops backwards on removing.  So instead of

for i=1,#lanes do &nbsp; lanes[i]:removeEventListener("touch", moveCar) end

instead to this

for i= #lanes, 1, -1 do &nbsp; lanes[i]:removeEventListener("touch", moveCar) end

especially when removing objects as once you remove child 1, child 2 then becomes child 1, etc.

Hi, thanks for the reply I will try your suggestion…any idea how to check the runtime object if nil or not? Thanks again

if obj then    or    if obj ~= nil then    either is fine

I tried to do that but still getting the same error…I ran a print on the lanes[i] within the loop…I had observed something weird…when the collision is between the enemy first object and player second…the code runs smooth and the table is also fine as shown below in corona console

22:24:18.913 enemy 22:24:18.913 player 22:24:18.913 userdata: 06D698B8 22:24:18.913 table: 0CD0A920 { 22:24:18.913 [id] =\> 3 22:24:18.913 [\_class] =\> table: 0CD0A920 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A870 22:24:18.913 [\_functionListeners] =\> table: 0CD0A920 { 22:24:18.913 [touch] =\> table: 0CD0AA10 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 } 22:24:18.913 table: 0CD0A808 { 22:24:18.913 [id] =\> 2 22:24:18.913 [\_class] =\> table: 0CD0A808 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A7F8 22:24:18.913 [\_functionListeners] =\> table: 0CD0A808 { 22:24:18.913 [touch] =\> table: 0CD0A6F0 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 } 22:24:18.913 table: 0CD0A998 { 22:24:18.913 [id] =\> 1 22:24:18.913 [\_class] =\> table: 0CD0A998 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A690 22:24:18.913 [\_functionListeners] =\> table: 0CD0A998 { 22:24:18.913 [touch] =\> table: 0CD0A830 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 }

However, when the collision is the other way around player and enemy then the output of lanes[i] is incomplete as if some part of the table (runtime part) just disappeared…the console output is below

22:24:45.147 player 22:24:45.147 enemy 22:24:45.147 userdata: 06D698B8 22:24:45.147 table: 0CD0A920 { 22:24:45.147 [id] =\> 3 22:24:45.147 [\_functionListeners] =\> table: 0CD0A920 { 22:24:45.147 [touch] =\> table: 0CD0AA10 { 22:24:45.147 [1] =\> function: 06D19088 22:24:45.147 } 22:24:45.147 } 22:24:45.147 } 22:24:45.147 ERROR: Runtime error&nbsp;

Any idea how why this may happen? Thanks

attempt to call method ‘removeEventListener’ (a nil value) = your object is already removed.

Always check an object is valid before you try and do anything to it.  Like this

if myObject then &nbsp;--do something with myObject end

FYI, It is always best practice to iterate loops backwards on removing.  So instead of

for i=1,#lanes do &nbsp; lanes[i]:removeEventListener("touch", moveCar) end

instead to this

for i= #lanes, 1, -1 do &nbsp; lanes[i]:removeEventListener("touch", moveCar) end

especially when removing objects as once you remove child 1, child 2 then becomes child 1, etc.

Hi, thanks for the reply I will try your suggestion…any idea how to check the runtime object if nil or not? Thanks again

if obj then    or    if obj ~= nil then    either is fine

I tried to do that but still getting the same error…I ran a print on the lanes[i] within the loop…I had observed something weird…when the collision is between the enemy first object and player second…the code runs smooth and the table is also fine as shown below in corona console

22:24:18.913 enemy 22:24:18.913 player 22:24:18.913 userdata: 06D698B8 22:24:18.913 table: 0CD0A920 { 22:24:18.913 [id] =\> 3 22:24:18.913 [\_class] =\> table: 0CD0A920 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A870 22:24:18.913 [\_functionListeners] =\> table: 0CD0A920 { 22:24:18.913 [touch] =\> table: 0CD0AA10 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 } 22:24:18.913 table: 0CD0A808 { 22:24:18.913 [id] =\> 2 22:24:18.913 [\_class] =\> table: 0CD0A808 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A7F8 22:24:18.913 [\_functionListeners] =\> table: 0CD0A808 { 22:24:18.913 [touch] =\> table: 0CD0A6F0 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 } 22:24:18.913 table: 0CD0A998 { 22:24:18.913 [id] =\> 1 22:24:18.913 [\_class] =\> table: 0CD0A998 { 22:24:18.913 [removeEventListener] =\> function: 06D19228 22:24:18.913 [addEventListener] =\> function: 06D191C8 22:24:18.913 [\_\_index] =\> table: 0CCAF800 { 22:24:18.913 \*table: 0CCAF800 22:24:18.913 } 22:24:18.913 } 22:24:18.913 [\_proxy] =\> userdata: 0CD0A690 22:24:18.913 [\_functionListeners] =\> table: 0CD0A998 { 22:24:18.913 [touch] =\> table: 0CD0A830 { 22:24:18.913 [1] =\> function: 01240688 22:24:18.913 } 22:24:18.913 } 22:24:18.913 }

However, when the collision is the other way around player and enemy then the output of lanes[i] is incomplete as if some part of the table (runtime part) just disappeared…the console output is below

22:24:45.147 player 22:24:45.147 enemy 22:24:45.147 userdata: 06D698B8 22:24:45.147 table: 0CD0A920 { 22:24:45.147 [id] =\> 3 22:24:45.147 [\_functionListeners] =\> table: 0CD0A920 { 22:24:45.147 [touch] =\> table: 0CD0AA10 { 22:24:45.147 [1] =\> function: 06D19088 22:24:45.147 } 22:24:45.147 } 22:24:45.147 } 22:24:45.147 ERROR: Runtime error&nbsp;

Any idea how why this may happen? Thanks