I am fairly new at lua and am trying to build a sidecroller for my first game. I ran into an error reading “Attempt to compare nil with number stack traceback”. It runs fine, collides with an enemy, shows the restart screen but then after starting again and colliding with another bomb it messes up. I included what I have so far I hope someone can help me, thanks!
What would be more helpful would be for you to copy and paste the whole error message from the console window that runs behind the simulator.
Rob
Also post the code here using code tags. But only post the relevant code (where the error is happening), not the entire project.
Two important reads:
https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/
http://docs.coronalabs.com/guide/basics/debugging/index.html
Rob
My bad still pretty new at this. What exactly is code tags?
The easiest way is to click the blue <> button in the bar with Bold, Italic, Underline, etc. when you’re typing in your message. It will popup window that you can paste your code in to.
My game is a jet sidescroller that has to avoid mines coming at it on the screen.
Okay so here is the error I’m gettting:
Runtime error
C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:196: attempt to call method ‘applyForce’ (a nil value)
stack traceback:
C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:196: in function <C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:195>
?: in function <?:221>
My game runs and when my plane collides with a mine it goes to a restart screen. As soon as I click the restart button, the error pops up. What I think is going on is that I messed it up with the removeScene method and somehow removed the jet and now doesn’t have anything to call activateJet
This is where the error is occurring:
--method to apply force to my jet in my game file function activateJet(self, event) self:applyForce(0, -1, self.x, self.y) --this is where the error is being thrown end
Any idea? Not sure if I did the removeScene methods right but I placed them like this:
--this is my scene:hide function where I removed the scene in my restart file. function scene:hide(event) title:removeEventListener("touch", start) composer.removeScene("restart") end --here is my scene:hide function where I removed the game scene in my game file function scene:hide(event) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("enterFrame", bgMtns1) Runtime:removeEventListener("enterFrame", bgMtns2) Runtime:removeEventListener("enterFrame", fgMtns1) Runtime:removeEventListener("enterFrame", fgMtns2) Runtime:removeEventListener("enterFrame", sky) Runtime:removeEventListener("enterFrame", sky2) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("collision", onCollision) composer.removeScene("game") end --the following is my jet code included inside the function scence:create(event) for my game file jetSpriteSheet = graphics.newImageSheet("jet.png", {width=50, height=17, numFrames=4}) jetSpriteSeq = {name="jets", start = 1, count=4, time=1000, loopCount=0} jet = display.newSprite(jetSpriteSheet, jetSpriteSeq ) sceneGroup:insert(jet) jet.anchorX = .5 jet.anchorY = .5 jet.x = -80 jet.y = 100 jet:play() jet.collided = false physics.addBody(jet, "static", {density=.1, bounce=.1, friction=.3, radius=12}) jetIntro = transition.to(jet,{time=2000, x=50, onComplete=jetReady}) --touchscreen function function touchScreen(event) --print("touch") if event.phase == "began" then print("began") jet.enterFrame = activateJet Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then print("ended") Runtime:removeEventListener("enterFrame", jet) end end
What would be more helpful would be for you to copy and paste the whole error message from the console window that runs behind the simulator.
Rob
Also post the code here using code tags. But only post the relevant code (where the error is happening), not the entire project.
Two important reads:
https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/
http://docs.coronalabs.com/guide/basics/debugging/index.html
Rob
My bad still pretty new at this. What exactly is code tags?
The easiest way is to click the blue <> button in the bar with Bold, Italic, Underline, etc. when you’re typing in your message. It will popup window that you can paste your code in to.
My game is a jet sidescroller that has to avoid mines coming at it on the screen.
Okay so here is the error I’m gettting:
Runtime error
C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:196: attempt to call method ‘applyForce’ (a nil value)
stack traceback:
C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:196: in function <C:\Users\Eric\Documents\Corona Projects\sidescroller\game.lua:195>
?: in function <?:221>
My game runs and when my plane collides with a mine it goes to a restart screen. As soon as I click the restart button, the error pops up. What I think is going on is that I messed it up with the removeScene method and somehow removed the jet and now doesn’t have anything to call activateJet
This is where the error is occurring:
--method to apply force to my jet in my game file function activateJet(self, event) self:applyForce(0, -1, self.x, self.y) --this is where the error is being thrown end
Any idea? Not sure if I did the removeScene methods right but I placed them like this:
--this is my scene:hide function where I removed the scene in my restart file. function scene:hide(event) title:removeEventListener("touch", start) composer.removeScene("restart") end --here is my scene:hide function where I removed the game scene in my game file function scene:hide(event) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("enterFrame", bgMtns1) Runtime:removeEventListener("enterFrame", bgMtns2) Runtime:removeEventListener("enterFrame", fgMtns1) Runtime:removeEventListener("enterFrame", fgMtns2) Runtime:removeEventListener("enterFrame", sky) Runtime:removeEventListener("enterFrame", sky2) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("collision", onCollision) composer.removeScene("game") end --the following is my jet code included inside the function scence:create(event) for my game file jetSpriteSheet = graphics.newImageSheet("jet.png", {width=50, height=17, numFrames=4}) jetSpriteSeq = {name="jets", start = 1, count=4, time=1000, loopCount=0} jet = display.newSprite(jetSpriteSheet, jetSpriteSeq ) sceneGroup:insert(jet) jet.anchorX = .5 jet.anchorY = .5 jet.x = -80 jet.y = 100 jet:play() jet.collided = false physics.addBody(jet, "static", {density=.1, bounce=.1, friction=.3, radius=12}) jetIntro = transition.to(jet,{time=2000, x=50, onComplete=jetReady}) --touchscreen function function touchScreen(event) --print("touch") if event.phase == "began" then print("began") jet.enterFrame = activateJet Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then print("ended") Runtime:removeEventListener("enterFrame", jet) end end