Thanks. Sorry for taking your time reading my long code. I solve it. Thank you,
You can’t just post a load of code and ask why it doesn’t work… you have to try and do some checking yourself!
This is bad though: you have two references to Runtime:addEventListener(“enterFrame”, pathScroll)
I am sorry. I am new to this. andi got zero knowledge about this. I just learn all of this by google it. i am out of ideas. I have been staying all night long. Few days now. and still can solve this. i already ask other questions, still no answer. i do check it. But still clueless. The two references. What i understand i have two “path” object that’s why i need to put two references. Do correct me if i am wrong. But don’t tell me that like i am not working hard to solve this. Because i do. i am sorry. I’ve got no option left. Thank you.
simplify your code until it works… when coding change one thing and then test if it works… if it does then add another (single) line of code and test… if it doesn’t then fix it until it works
you get the point
hi i do what you said. And I know this is maybe still not the simplest. But can you take a look ?why the car when in objectCollide/Collision it get this error.
Attempt to index a nil value
but when the collision with coin it doesn’t give that error. why?? Can you please explain and point out the wrong in here? i am sorry for asking too much. but thank you so much.
---REQUIRESlocal composer = require( "composer" ) local scene = composer.newScene() composer.recycleOnSceneChange = true local widget = require( "widget" ) local physics = require "physics" physics.start() local sendEnemyCar,createCoin,createCar,objectCollide,updateTimer,endGame local obj1 local obj2 local lanes = {} local laneID = 2 local lanes\_H = {} local enemyCars = {} local enemyCounter = 0 local Car={} local CarCounter = 0 local sendCarFrequency = 2500 local timeDisplay local tmr\_startGame,tmrToSendCars,tmrToSendCoin local coins = {} local coinsCounter = 0 local distance = 0 local score = 0 local scoreText local txt\_counter local scroll = 2 -- EFM local RightSide -- EFM local LeftSide -- EFM local rightLabel -- EFM local leftLabel -- EFM local path1 -- EFM local path2 -- EFM local ground local centerlane = (side/8) - 4 ---------------------- ---------------------- ---RUN COIN createCar = function () Car[CarCounter] = display.newImageRect("Cars/Kereta"..math.random(1,11)..".png", 150, 200) Car[CarCounter].x = lanes[math.random(1,#lanes)].x Car[CarCounter].y = -125 Car[CarCounter].id = "car" physics.addBody(Car[CarCounter], {bounce = 0, friction = 1, density = 0}) print (Car) Car[CarCounter].bodyType = "kinematic" transition.to(Car[CarCounter], {y=display.contentHeight+Car[CarCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); 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. CarCounter = CarCounter + 1 print (Car) end ---------------------- ---RUN COIN createCoin = function () coins[coinsCounter] = display.newImageRect("Point/coin1.png", 70, 70) coins[coinsCounter].x = lanes[math.random(1,#lanes)].x coins[coinsCounter].y = -125 coins[coinsCounter].id = "coin" physics.addBody(coins[coinsCounter], {bounce = 0, friction = 1, density = 0}) print (coins) coins[coinsCounter].bodyType = "kinematic" transition.to(coins[coinsCounter], {y=display.contentHeight+coins[coinsCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); 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. coinsCounter = coinsCounter + 1 print (coins) end ---------------------- ---COLLISION objectCollide = function (event) obj1 = event.object1 -- store the first object obj2 = event.object2 -- store the second object if(event.phase == "began") then -- when the enemy car collides into the player car, this if/then statement will be true print ("collision") print(obj1) print(coins) print(Car) if(obj1.id == "chick" and obj2.id == "car") then -- stop the game transition.pause() timer.cancel(tmrToSendCars) timer.cancel(tmrToSendCoin) physics.pause() -- remove event listeners from all lanes for i=1,#lanes do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end for i=1,enemyCounter do if(enemyCars[i]) then display.remove(enemyCars[i]) end end for i=1,coinsCounter do if(coins[i]) then display.remove(coins[i]) end end --display.remove(coins[i]) display.remove(obj1) display.remove(obj2) timer.performWithDelay( 0, endGame ) end if(obj1.id == "chick" and obj2.id == "coin") then display.remove(obj2) score =score + 1 print ("Score: " .. score) scoreText.text = "Score: " .. score end end end ---------------------- updateTimer = function (event) distance = distance + 1 timeDisplay.text = distance if distance == 10 then print("Change background") -- stop the game --transition.pause() --timer.cancel(tmrToSendCars) --timer.cancel(tmrToSendCoin) --physics.pause() --changeScene() end end ---END GAME endGame = function (event) --composer.setVariable( "finalScore", score ) --composer.setVariable( "finalScore1", score ) --distance=0 print("end leave game.lua") --Runtime:removeEventListener("enterFrame", pathScroll) Runtime:removeEventListener("enterFrame", pathScroll) RightSide:removeEventListener( "tap" ) LeftSide:removeEventListener( "tap" ) Runtime:removeEventListener( "collision", onCollision ) composer.gotoScene( "restart1", { time=800, effect="crossFade" } ) end ---------------------- ---CREATE -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. function scene:create( ) local group = self.view local background = display.newGroup() -- EFM local road = display.newGroup() -- EFM local content = display.newGroup() -- EFM local interface = display.newGroup() -- EFM group:insert(background) group:insert(road) group:insert(content) group:insert(interface) ---TAP BACKGROUND --print(car) print(coins) LeftSide = display.newImageRect(background, "fillT.png", fullw/2, fullh ) -- EFM LeftSide.anchorX = 0 -- EFM LeftSide.x = left -- EFM LeftSide.y = centerY -- EFM function LeftSide.tap( self, event ) -- EFM print("Tapped Left Side") if laneID \> 1 then laneID = laneID - 1; transition.cancel(chick) local function onComplete() print( "At lane " .. laneID .. " to the left" ) end transition.to( chick, { x = lanes[laneID].x, time = 50, onComplete = onComplete } ) end return true end RightSide = display.newImageRect(background, "fillT.png", fullw/2, fullh ) -- EFM RightSide.anchorX = 1 -- EFM RightSide.x = right -- EFM RightSide.y = centerY -- EFM function RightSide.tap( self, event ) -- EFM print("Tapped Right Side") if laneID \< 3 then laneID = laneID + 1; transition.cancel(chick) local function onComplete() print( "At lane " .. laneID .. " to the right" ) end transition.to( chick, { x = lanes[laneID].x, time = 50, onComplete = onComplete } ) end return true end ---------------------- ---LABEL TAP rightLabel = display.newText( { text = "", x = 0, y = 0 , fontSize = 50 } ) interface:insert( rightLabel) rightLabel:setTextColor( 0 ) rightLabel.x = right - 50 rightLabel.y = centerY leftLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 50 } ) interface:insert( leftLabel ) leftLabel:setTextColor( 0 ) leftLabel.x = left + 50 leftLabel.y = centerY ---------------------- ---PATHWAY (BACKGROUND) path1 = display.newImageRect( road, "road/theroad1.png", fullw, fullh ) path1.x = centerX path1.y = centerY path2 = display.newImageRect( road, "road/theroad2.png", fullw, fullh ) path2.x = centerX path2.y = centerY - fullh ---------------------- ---INVISIBLE GROUND ground = display.newImageRect(background, "fillT.png", 320, 70) ground.x = display.contentWidth\*0.5 ground.y = display.contentHeight physics.addBody(ground, "static" , { friction=0.5, bounce=0 }) ---------------------- --(centerX - (side + 125) ---LANES for i = 1, 3 do -- loop 3 times to create 3 lanes for our game laneimg = display.newImageRect( road, "fillT.png", side-40, fullh ) -- EFM lanes[i] = laneimg lanes[i].x = (side/4) + (i\*(side-centerlane)) --((i)\*(side)) lanes[i].y = centerY lanes[i].id = i end ---------------------- ---LANES HORIZONTAL for r = 1, 10 do -- loop 3 times to create 3 lanes for our game laneHimg = display.newImageRect( road, "fillT.png", fullw, fullh/30 ) -- EFM lanes\_H[r] = laneHimg lanes\_H[r].x = centerX lanes\_H[r].y = r\*((bottom - centerX)/8) lanes\_H[r].id = r end ---------------------- ---CHICK chick = display.newImageRect( content, "Player/"..math.random(1,10)..".png",100,100) chick.x = lanes[2].x chick.y = lanes\_H[9].y chick.id = "chick" physics.addBody( chick, "dynamic" ,{ isSensor=true }) ---------------------- timeDisplay = display.newText(content,distance,0,0,native.systemFrontBold,64) timeDisplay.x = centerX +200 timeDisplay.y = top + 50 scoreText = display.newText( content, "Score: " .. score, 400, 80, native.systemFont, 36 ) end ---------------------- ---PATHWAY SCROLL function pathScroll (self,event) path1.y = path1.y + scroll path2.y = path2.y + scroll if path1.y == fullh \* 2.5 then path1.y = fullh \* -.5 end if path2.y == fullh \* 2.5 then path2.y = fullh \* -.5 end end ---------------------- -- EFM HAS TWO PHASES \>\>\> "will" and "did" ---SHOW --that will show in scene function scene:show (event) if( event.phase == "will" ) then elseif( event.phase == "did" ) then local function startGame() ---FOR PATHWAY TO SCROLL path1.enterFrame = pathScroll path2.enterFrame = pathScroll Runtime:addEventListener("enterFrame", pathScroll) ---------------------- ---WHEN TAP TO RIGHT RightSide:addEventListener( "tap" ) rightLabel.text = "right" ---------------------- ---WHEN TAP TO LEFT LeftSide:addEventListener( "tap" ) leftLabel.text = "left" ---------------------- end timer.performWithDelay(1000, updateTimer, 0) tmr\_startGame = timer.performWithDelay(0, startGame, 1) -- allow the player to move shortly after the game has started tmrToSendCoin = timer.performWithDelay(1500, createCoin, 0) tmrToSendCar = timer.performWithDelay(1500, createCar, 0) tmrToSendCars = timer.performWithDelay(sendEnemyFrequency, sendEnemyCar, 0) Runtime:addEventListener( "collision", objectCollide ) -- create a global event listener to listen for any collision end end ---------------------- ---HIDE --function scene:hide (event) --local sceneGroup = self.view -- if( event.phase == "will" ) then -- elseif( event.phase == "did" ) then -- end --end ---------------------- -- destroy() function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "enter", scene ) scene:addEventListener( "show", scene ) --scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
okayi do know why. because it cannot pass that endgame function. so how to pass it? can you at least give me clue. please.
Exact error messages and line numbers would be a good start. You’re expecting people to take 30+ minutes out of their day to read all your (uncommented) code, figure out how it works (without being able to run it), and then fix it. Someone might find some time eventually, but you might have to be patient.
For a start code like this will likely cause errors
for i=1,#lanes do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end
you should walk it backwards instead
for i=#lanes, 1, -1 do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end
I have to agree with the @nick_sherman. Dumping tons of code with no reference to the problem doesn’t help people solve your problem.
It’s really important to read this post: https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/
and follow it’s advice.
There is a console log window that opens with the simulator, copy and pasting the entire error stack trace is helpful. Then let us know which line of code is the problem and maybe just post enough code to have the error line and a few lines above it. Maybe just the whole block or function will do to start with.
Rob
You can’t just post a load of code and ask why it doesn’t work… you have to try and do some checking yourself!
This is bad though: you have two references to Runtime:addEventListener(“enterFrame”, pathScroll)
I am sorry. I am new to this. andi got zero knowledge about this. I just learn all of this by google it. i am out of ideas. I have been staying all night long. Few days now. and still can solve this. i already ask other questions, still no answer. i do check it. But still clueless. The two references. What i understand i have two “path” object that’s why i need to put two references. Do correct me if i am wrong. But don’t tell me that like i am not working hard to solve this. Because i do. i am sorry. I’ve got no option left. Thank you.
simplify your code until it works… when coding change one thing and then test if it works… if it does then add another (single) line of code and test… if it doesn’t then fix it until it works
you get the point
hi i do what you said. And I know this is maybe still not the simplest. But can you take a look ?why the car when in objectCollide/Collision it get this error.
Attempt to index a nil value
but when the collision with coin it doesn’t give that error. why?? Can you please explain and point out the wrong in here? i am sorry for asking too much. but thank you so much.
---REQUIRESlocal composer = require( "composer" ) local scene = composer.newScene() composer.recycleOnSceneChange = true local widget = require( "widget" ) local physics = require "physics" physics.start() local sendEnemyCar,createCoin,createCar,objectCollide,updateTimer,endGame local obj1 local obj2 local lanes = {} local laneID = 2 local lanes\_H = {} local enemyCars = {} local enemyCounter = 0 local Car={} local CarCounter = 0 local sendCarFrequency = 2500 local timeDisplay local tmr\_startGame,tmrToSendCars,tmrToSendCoin local coins = {} local coinsCounter = 0 local distance = 0 local score = 0 local scoreText local txt\_counter local scroll = 2 -- EFM local RightSide -- EFM local LeftSide -- EFM local rightLabel -- EFM local leftLabel -- EFM local path1 -- EFM local path2 -- EFM local ground local centerlane = (side/8) - 4 ---------------------- ---------------------- ---RUN COIN createCar = function () Car[CarCounter] = display.newImageRect("Cars/Kereta"..math.random(1,11)..".png", 150, 200) Car[CarCounter].x = lanes[math.random(1,#lanes)].x Car[CarCounter].y = -125 Car[CarCounter].id = "car" physics.addBody(Car[CarCounter], {bounce = 0, friction = 1, density = 0}) print (Car) Car[CarCounter].bodyType = "kinematic" transition.to(Car[CarCounter], {y=display.contentHeight+Car[CarCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); 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. CarCounter = CarCounter + 1 print (Car) end ---------------------- ---RUN COIN createCoin = function () coins[coinsCounter] = display.newImageRect("Point/coin1.png", 70, 70) coins[coinsCounter].x = lanes[math.random(1,#lanes)].x coins[coinsCounter].y = -125 coins[coinsCounter].id = "coin" physics.addBody(coins[coinsCounter], {bounce = 0, friction = 1, density = 0}) print (coins) coins[coinsCounter].bodyType = "kinematic" transition.to(coins[coinsCounter], {y=display.contentHeight+coins[coinsCounter].height+20, time=math.random(2250,3000), onComplete=function(self) display.remove(self); 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. coinsCounter = coinsCounter + 1 print (coins) end ---------------------- ---COLLISION objectCollide = function (event) obj1 = event.object1 -- store the first object obj2 = event.object2 -- store the second object if(event.phase == "began") then -- when the enemy car collides into the player car, this if/then statement will be true print ("collision") print(obj1) print(coins) print(Car) if(obj1.id == "chick" and obj2.id == "car") then -- stop the game transition.pause() timer.cancel(tmrToSendCars) timer.cancel(tmrToSendCoin) physics.pause() -- remove event listeners from all lanes for i=1,#lanes do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end for i=1,enemyCounter do if(enemyCars[i]) then display.remove(enemyCars[i]) end end for i=1,coinsCounter do if(coins[i]) then display.remove(coins[i]) end end --display.remove(coins[i]) display.remove(obj1) display.remove(obj2) timer.performWithDelay( 0, endGame ) end if(obj1.id == "chick" and obj2.id == "coin") then display.remove(obj2) score =score + 1 print ("Score: " .. score) scoreText.text = "Score: " .. score end end end ---------------------- updateTimer = function (event) distance = distance + 1 timeDisplay.text = distance if distance == 10 then print("Change background") -- stop the game --transition.pause() --timer.cancel(tmrToSendCars) --timer.cancel(tmrToSendCoin) --physics.pause() --changeScene() end end ---END GAME endGame = function (event) --composer.setVariable( "finalScore", score ) --composer.setVariable( "finalScore1", score ) --distance=0 print("end leave game.lua") --Runtime:removeEventListener("enterFrame", pathScroll) Runtime:removeEventListener("enterFrame", pathScroll) RightSide:removeEventListener( "tap" ) LeftSide:removeEventListener( "tap" ) Runtime:removeEventListener( "collision", onCollision ) composer.gotoScene( "restart1", { time=800, effect="crossFade" } ) end ---------------------- ---CREATE -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. function scene:create( ) local group = self.view local background = display.newGroup() -- EFM local road = display.newGroup() -- EFM local content = display.newGroup() -- EFM local interface = display.newGroup() -- EFM group:insert(background) group:insert(road) group:insert(content) group:insert(interface) ---TAP BACKGROUND --print(car) print(coins) LeftSide = display.newImageRect(background, "fillT.png", fullw/2, fullh ) -- EFM LeftSide.anchorX = 0 -- EFM LeftSide.x = left -- EFM LeftSide.y = centerY -- EFM function LeftSide.tap( self, event ) -- EFM print("Tapped Left Side") if laneID \> 1 then laneID = laneID - 1; transition.cancel(chick) local function onComplete() print( "At lane " .. laneID .. " to the left" ) end transition.to( chick, { x = lanes[laneID].x, time = 50, onComplete = onComplete } ) end return true end RightSide = display.newImageRect(background, "fillT.png", fullw/2, fullh ) -- EFM RightSide.anchorX = 1 -- EFM RightSide.x = right -- EFM RightSide.y = centerY -- EFM function RightSide.tap( self, event ) -- EFM print("Tapped Right Side") if laneID \< 3 then laneID = laneID + 1; transition.cancel(chick) local function onComplete() print( "At lane " .. laneID .. " to the right" ) end transition.to( chick, { x = lanes[laneID].x, time = 50, onComplete = onComplete } ) end return true end ---------------------- ---LABEL TAP rightLabel = display.newText( { text = "", x = 0, y = 0 , fontSize = 50 } ) interface:insert( rightLabel) rightLabel:setTextColor( 0 ) rightLabel.x = right - 50 rightLabel.y = centerY leftLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 50 } ) interface:insert( leftLabel ) leftLabel:setTextColor( 0 ) leftLabel.x = left + 50 leftLabel.y = centerY ---------------------- ---PATHWAY (BACKGROUND) path1 = display.newImageRect( road, "road/theroad1.png", fullw, fullh ) path1.x = centerX path1.y = centerY path2 = display.newImageRect( road, "road/theroad2.png", fullw, fullh ) path2.x = centerX path2.y = centerY - fullh ---------------------- ---INVISIBLE GROUND ground = display.newImageRect(background, "fillT.png", 320, 70) ground.x = display.contentWidth\*0.5 ground.y = display.contentHeight physics.addBody(ground, "static" , { friction=0.5, bounce=0 }) ---------------------- --(centerX - (side + 125) ---LANES for i = 1, 3 do -- loop 3 times to create 3 lanes for our game laneimg = display.newImageRect( road, "fillT.png", side-40, fullh ) -- EFM lanes[i] = laneimg lanes[i].x = (side/4) + (i\*(side-centerlane)) --((i)\*(side)) lanes[i].y = centerY lanes[i].id = i end ---------------------- ---LANES HORIZONTAL for r = 1, 10 do -- loop 3 times to create 3 lanes for our game laneHimg = display.newImageRect( road, "fillT.png", fullw, fullh/30 ) -- EFM lanes\_H[r] = laneHimg lanes\_H[r].x = centerX lanes\_H[r].y = r\*((bottom - centerX)/8) lanes\_H[r].id = r end ---------------------- ---CHICK chick = display.newImageRect( content, "Player/"..math.random(1,10)..".png",100,100) chick.x = lanes[2].x chick.y = lanes\_H[9].y chick.id = "chick" physics.addBody( chick, "dynamic" ,{ isSensor=true }) ---------------------- timeDisplay = display.newText(content,distance,0,0,native.systemFrontBold,64) timeDisplay.x = centerX +200 timeDisplay.y = top + 50 scoreText = display.newText( content, "Score: " .. score, 400, 80, native.systemFont, 36 ) end ---------------------- ---PATHWAY SCROLL function pathScroll (self,event) path1.y = path1.y + scroll path2.y = path2.y + scroll if path1.y == fullh \* 2.5 then path1.y = fullh \* -.5 end if path2.y == fullh \* 2.5 then path2.y = fullh \* -.5 end end ---------------------- -- EFM HAS TWO PHASES \>\>\> "will" and "did" ---SHOW --that will show in scene function scene:show (event) if( event.phase == "will" ) then elseif( event.phase == "did" ) then local function startGame() ---FOR PATHWAY TO SCROLL path1.enterFrame = pathScroll path2.enterFrame = pathScroll Runtime:addEventListener("enterFrame", pathScroll) ---------------------- ---WHEN TAP TO RIGHT RightSide:addEventListener( "tap" ) rightLabel.text = "right" ---------------------- ---WHEN TAP TO LEFT LeftSide:addEventListener( "tap" ) leftLabel.text = "left" ---------------------- end timer.performWithDelay(1000, updateTimer, 0) tmr\_startGame = timer.performWithDelay(0, startGame, 1) -- allow the player to move shortly after the game has started tmrToSendCoin = timer.performWithDelay(1500, createCoin, 0) tmrToSendCar = timer.performWithDelay(1500, createCar, 0) tmrToSendCars = timer.performWithDelay(sendEnemyFrequency, sendEnemyCar, 0) Runtime:addEventListener( "collision", objectCollide ) -- create a global event listener to listen for any collision end end ---------------------- ---HIDE --function scene:hide (event) --local sceneGroup = self.view -- if( event.phase == "will" ) then -- elseif( event.phase == "did" ) then -- end --end ---------------------- -- destroy() function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "enter", scene ) scene:addEventListener( "show", scene ) --scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
okayi do know why. because it cannot pass that endgame function. so how to pass it? can you at least give me clue. please.
Exact error messages and line numbers would be a good start. You’re expecting people to take 30+ minutes out of their day to read all your (uncommented) code, figure out how it works (without being able to run it), and then fix it. Someone might find some time eventually, but you might have to be patient.
For a start code like this will likely cause errors
for i=1,#lanes do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end
you should walk it backwards instead
for i=#lanes, 1, -1 do lanes[i]:removeEventListener("touch", sendEnemyCar) lanes[i]:removeEventListener("touch", createCoin) end
I have to agree with the @nick_sherman. Dumping tons of code with no reference to the problem doesn’t help people solve your problem.
It’s really important to read this post: https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/
and follow it’s advice.
There is a console log window that opens with the simulator, copy and pasting the entire error stack trace is helpful. Then let us know which line of code is the problem and maybe just post enough code to have the error line and a few lines above it. Maybe just the whole block or function will do to start with.
Rob