Attempt to compare nil with number in game [duplicate]

ε祈祈猫儿з 提交于 2019-12-25 04:12:11

问题


hey guys i am about to finish my game i am a indie developer and i i am trying to finish my app in corona sdk and i keep getting this annoying error message. The message is get is "Attempt to compare nil with number restart.lua 9 " i don't have a clue how to fix this i try every thing but nothing works. when i run my code and i die for the first time and restart the game it work fine but the second time the player die ,and i try to restart the game i get this error message. this is the code

okay i want to be clear about this so i am going to post my hole code in the game 2 file so you guys can understand what i am going at.

-----------------------------------------------------------------------------------------

-- main.lua

  ---local  titlebar = "no title bar"
   -- requirs physics 

 --variable to hold our game's score
   local score = 0


    local physics = require "physics"
   physics.start() 

   require "sprite"

    local storyboard = require("storyboard")
      local scene = storyboard.newScene()


    function scene:createScene(event)
     local screenGroup = self.view

    -- background 
     local background = display.newImage("background01.png")
     screenGroup:insert(background)

    -- local background = display.newImageRect( "background01.png", 570, 360 )

 --ship =display.newImage("empire 2.png")

 top1 = display.newImage("invisable tile.png")
 top1:setReferencePoint(display.BottomLeftReferencePoint)
 top1.x=0
 top1.y= -200
 physics.addBody(top1, "static", {density=.1,bounce=0.1, friction=.2})
 screenGroup:insert(top1)


 top2 = display.newImage("invisable tile.png")
 top2:setReferencePoint(display.BottomLeftReferencePoint)
 top2.x =0
 top2.y= 400
 physics.addBody(top2, "static", {density=.1,bounce=0.1, friction=.2})
 screenGroup:insert(top2)






 moon = display.newImage("moon.png")
 moon:setReferencePoint(display.BottomLeftReferencePoint)
 moon.x =400
 moon.y=100
 moon.speed = 4
screenGroup:insert(moon)

   moon2 = display.newImage("moon.png")
   moon2: setReferencePoint(display.BottomLeftReferencePoint)
   moon2.x =1090
   moon2.y=100
   moon2.speed = 4
   screenGroup:insert(moon2)

    -- this copy to the next scroll secne --empire should be change varable
     empire3 = display.newImage("empire1.png")
     empire3: setReferencePoint(display.BottomLeftReferencePoint)                   
     empire3.x =1400
     empire3.y=450
     empire3.speed = 7
  screenGroup:insert(empire3)

   -- local empire2 = display.newImage("empire 2.png")
   --  empire2: setReferencePoint(display.BottomLeftReferencePoint)
   -- empire2.x = 400
   -- empire2.y= 400
   -- empire2.speed = 10

    -- local empire4 = display.newImage("empire 2.png") -- copy of empire 2 
    -- empire4: setReferencePoint(display.BottomLeftReferencePoint)
   --empire4.x = 1090
   -- empire4.y= 400
    -- empire4.speed = 10
    -- this copy to the next scroll secne 

    empire1 =display.newImage("empire1.png")
   empire1: setReferencePoint(display.BottomLeftReferencePoint)
   empire1.x =570
   empire1.y= 450
   empire1.speed = 7
     screenGroup:insert(empire1)

    jetSpriteSheet = sprite.newSpriteSheet("jet11.png",50,17)
    jetSprites = sprite.newSpriteSet(jetSpriteSheet,1,4)
    sprite.add(jetSprites,"jets",1,4,5000,0)-- timer
     jet=sprite.newSprite(jetSprites)
    jet.y = 40
    jet.x = -80 
    jet:prepare("jets")
   jet.collided = false
    jet:play()
    physics.addBody(jet, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
    screenGroup:insert(jet)
     jetIntro = transition.to(jet,{time=6000, x=100, onComplete=jetReady})

    explosionSpriteSheet = sprite.newSpriteSheet("explosion.png", 24, 23)
    explosionSprites = sprite.newSpriteSet(explosionSpriteSheet, 1, 8)
    sprite.add(explosionSprites, "explosions", 1, 8, 2000, 1)
    explosion = sprite.newSprite(explosionSprites)
    explosion.x = 100
    explosion.y = 100
    explosion:prepare("explosions")
    explosion:play()
     explosion.isVisible = false
     -- physics.addBody(jet, "dynamic", {density=.1, bounce=0.1, friction=.2, radius=12})
    screenGroup:insert(explosion)



    mine1 = display.newImage("rocket01.png")
    mine1.x = 500
    mine1.y = 200
    mine1.speed = math.random(2,6)
   -- mine1.initY = mine1.y
  --  mine1.amp = math.random(20,100)
   -- mine1.angle = math.random(1,360)
    physics.addBody(mine1, "static", {density=.1, bounce=0.1, friction=.2, radius=12})
    screenGroup:insert(mine1)

    mine2 = display.newImage("missles.png")
    mine2.y = 100--math.random (1,100)
    mine2.x = 1090
    mine2.speed = math.random(2,6)
-- mine2.amp = math.random(2,6)     
   --mine2.initY = mine2.y

    physics.addBody(mine2, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
    screenGroup:insert(mine2)



    mine3 = display.newImage("missles.png")
    mine3.y = 100
    mine3.x = 500
    mine3.speed = math.random(2,6)
   -- mine3.initY = mine1.y
     --  mine3.amp   = math.random(20,100)
    mine3.angle = math.random(1,360)    

    physics.addBody(mine3, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
    screenGroup:insert(mine3)

  mine4 = display.newImage("rocket01.png")
    mine4.y = 100
    mine4.x = 500
    mine4.speed = math.random(2,6)
   -- mine3.initY = mine1.y
  --  mine3.amp   = math.random(20,100)
 mine3.angle = math.random(1,360)   
  screenGroup:insert(mine4)






 physics.addBody(mine4, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
 screenGroup:insert(mine4)

   coin04 = display.newImage("coin04.png")
   coin04.y = 100
   coin04.x = 500
   coin04.speed = math.random(2,6)
   coin04.initY = mine1.y
   coin04.amp   = math.random(20,100)
  coin04.angle = math.random(1,360) 

 physics.addBody(coin04, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
 screenGroup:insert(coin04)




  rocket02 = display.newImage("rocket02.png")
  rocket02.y = 100
  rocket02.x = 500
  rocket02.speed = math.random(2,6)
  rocket02.initY = mine1.y
  rocket02.amp   = math.random(20,100)
  rocket02.angle = math.random(1,360)   

 physics.addBody(rocket02, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(rocket02)


  rocket03 = display.newImage("rocket02.png")
  rocket03.y = 100
  rocket03.x = 500
  rocket03.speed = math.random(2,6)
  rocket03.initY = mine1.y
  rocket03.amp   = math.random(20,100)
  rocket03.angle = math.random(1,360)   

 physics.addBody(rocket03, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(rocket03)


  rocket04 = display.newImage("rocket02.png")
  rocket04.y = 100
  rocket04.x = 500
  rocket04.speed = math.random(2,6)
  rocket04.initY = mine1.y
  rocket04.amp   = math.random(20,100)
  rocket04.angle = math.random(1,360)   

 physics.addBody(rocket04, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(rocket04)


 ball01 = display.newImage("ball01.png")
  ball01.y = 100
  ball01.x = 500
  ball01.speed = math.random(2,6)
  ball01.initY = mine1.y
  ball01.amp   = math.random(20,100)
  ball01.angle = math.random(1,360) 
  rotation = 2 
 physics.addBody(ball01, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(ball01)



  ball02 = display.newImage("ball01.png")
  ball02.y = 100
  ball02.x = 500
  ball02.speed = math.random(2,6)
  ball02.initY = mine1.y
  ball02.amp   = math.random(20,100)
  ball02.angle = math.random(1,360) 
  rotation = 2 
 physics.addBody(ball02, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(ball02)


   ball03 = display.newImage("ball02.png")
  ball03.y = 100
  ball03.x = 500
  ball03.speed = math.random(2,6)
  --ball03.initY = mine1.y
 -- ball03.amp   = math.random(20,100)
  ball03.angle = math.random(1,360) 
  rotation = 2 
 physics.addBody(ball03, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(ball03)


  ball04 = display.newImage("ball02.png")
  ball04.y = 100
  ball04.x = 500
  ball04.speed = math.random(2,6)
  ball04.initY = mine1.y
  ball04.amp   = math.random(20,100)
  ball04.angle = math.random(1,360) 
  rotation = 2 
 physics.addBody(ball04, "static", {density=.1,bounce=0.1, friction=.2,radius=12})

 screenGroup:insert(ball04)


  coin01 = display.newImage("coin02.png")
  coin01.y = 100
  coin01.x = 500
   coin01.speed = math.random(2,6)
  coin01.initY = mine1.y
   coin01.amp   = math.random(20,100)
   coin01.angle = math.random(1,360)    



 physics.addBody(coin01, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
 screenGroup:insert(coin01)




    scoreText = display.newText("score: " .. score, 0, 0, "showcardgothic", 14)

scoreText:setTextColor(225,225,225)
scoreText.x = 10
scoreText.y = 20
screenGroup:insert(scoreText)


pause = display.newImage("pause.png")
pause.x = 480
pause.y = 289
screenGroup:insert(pause)



 end  -- end of create scene



local function scoreUpdate()

 score =score +10 
 scoreText.text = "score:"..score
---scoreText:setTextColor(225,225,225)
-- scoreText:setReferencePoint(display.CenterLeftReferencePoint)
 scoreText.x = 10 
 scoreText.y =20    

end

 timer1 = timer.performWithDelay( 1000, scoreUpdate, - 1 )



 function pause01 (event)
 if event.phase =="began" then 
  --print "hi"

  function jetReady()
    jet.bodyType = "static"
end

storyboard.gotoScene("pause menu","crossFade",400)  

 result = timer.pause( timer1 )
print( "Time remaining is ")

   end
 end 

this code that is below that is bold is the code that's giving problem hope you guys can tell me whats wrong..

***print "passed"***

 ***function scrollSpace(self,event)
 if self.x  < -900 then
  self.x = 800
else 
self.x = self.x - self.speed
 end 
    end***


  local function moveMines(self,event)
  if self.x  < -900 then
   self.x = 489            --800

   self.y = math.random(90,220)
   self.x = 500
   self.speed = math.random(2,6)
--  self.amp = math.random(20,100)
--  self.angle = math.random(1,360)

 else 
    self.x = self.x - self.speed
    -- self.angle = self.angle + .1
    -- self.y = self.amp * math.sin(self.angle)+ self.initY
        end 
    end



 local function coins(self,event)
  if self.x  < -900 then
   self.x = 800

   self.y = math.random(90,100)
   self.x = 500
   self.speed = math.random(2,6)
  self.amp = math.random(20,100)
 self.angle = math.random(1,360)

    else 
    self.x = self.x - self.speed
     self.angle = self.angle + .1
     self.y = self.amp * math.sin(self.angle)+ self.initY
        end 
    end 

      --     moveMissile1


   function jetReady()
    jet.bodyType = "dynamic"
    end


   function activateJets(self,event)
     self:applyForce(0,-1.5,self.x,self.y)
    end

    function touchScreen(event)
    --print("touch")
    if event.phase == "began"then
    jet.enterFrame = activateJets 
    Runtime:addEventListener ("enterFrame",jet)
     end

   if event.phase == "ended"then
   --print ("ended")
   Runtime:removeEventListener ("enterFrame",jet)
   end

   end

   function gameOver()
   storyboard.gotoScene("restart", "fade", 400)
   end

   function explode()

    explosion.x = jet.x
    explosion.y = jet.y
    explosion.isVisible = true
    explosion:play()
    jet.isVisible = false
    timer.performWithDelay(3000, gameOver, 1)

   end


   function onCollision(event)
    if event.phase == "began" then
      if jet.collided == false then 
        jet.collided = true
        jet.bodyType = "static"
         result =  timer.pause(timer1 )
        explode()
        --storyboard.gotoScene("restart", "fade", 400)
      end
    end

    end


   function scene:enterScene(event)




  moon.enterFrame = scrollSpace
 Runtime:addEventListener ("enterFrame",moon)

     pause:addEventListener("touch",pause01)

     empire3.enterFrame = scrollSpace
     Runtime:addEventListener ("enterFrame",empire3)

     moon2.enterFrame = scrollSpace
     Runtime:addEventListener ("enterFrame",moon2)

    mine1.enterFrame = moveMines
    Runtime:addEventListener ("enterFrame",mine1)

     mine2.enterFrame = moveMines
         Runtime:addEventListener ("enterFrame",mine2)

     mine3.enterFrame = moveMines
       Runtime:addEventListener ("enterFrame",mine3)

     mine4.enterFrame = moveMines
    Runtime:addEventListener ("enterFrame",mine4)   

   rocket02 .enterFrame = coins
     Runtime:addEventListener ("enterFrame",rocket02)   

     coin04.enterFrame = coins
    Runtime:addEventListener ("enterFrame",coin04)

     coin01.enterFrame = coins
    Runtime:addEventListener ("enterFrame",coin01)  

    rocket03 .enterFrame = coins
    Runtime:addEventListener ("enterFrame",rocket03)

     rocket04 .enterFrame = moveMines
    Runtime:addEventListener ("enterFrame",rocket04)


   ball01 .enterFrame = coins
    Runtime:addEventListener ("enterFrame",ball01)

   ball02 .enterFrame = coins
    Runtime:addEventListener ("enterFrame",ball02)  

     ball03 .enterFrame = moveMines
    Runtime:addEventListener ("enterFrame",ball03)

    ball04 .enterFrame = coins
   Runtime:addEventListener ("enterFrame",ball04)   

 -- empire2.enterFrame = scrollSpace
 --Runtime:addEventListener ("enterFrame",empire2)

    --  empire4.enterFrame = scrollSpace
-- Runtime:addEventListener ("enterFrame",empire4)

 empire1.enterFrame = scrollSpace
 Runtime:addEventListener ("enterFrame",empire1)

 Runtime:addEventListener("touch",touchScreen)

  Runtime:addEventListener("collision",onCollision) 


  end 


  function scene:exitScene(event)

  Runtime:removeEventListener("touch",touchScreen)
  Runtime:removeEventListener ("enterFrame",moon)
  Runtime:removeEventListener ("enterFrame",empire3)
  Runtime:removeEventListener ("enterFrame",moon2)
  Runtime:removeEventListener ("enterFrame",mine1)
  Runtime:removeEventListener ("enterFrame",mine2)
  Runtime:removeEventListener ("enterFrame",mine3)
  Runtime:removeEventListener ("enterFrame",mine4)
  Runtime:removeEventListener ("enterFrame",coin04)
  Runtime:removeEventListener ("enterFrame",coin01)
  Runtime:removeEventListener ("enterFrame",empire1)
  Runtime:removeEventListener("collision",onCollision)  
  Runtime: removeEventListener ("enterFrame",rocket02)
  end

   function scene:destroyScene(event)

   end




     scene:addEventListener("createScene",scene)
     scene:addEventListener("enterScene",scene) 
     scene:addEventListener("exitScene",scene) 
     scene:addEventListener("destroyScene",scene) 

     return scene

回答1:


Apparently, self.x doesn't exist. Make sure that you initialize it somewhere before that compare at line 9. I think you are lucky, these kinds of bugs are usually solved easily..

If you still can't figure it out, post where you first initialize self.x, could be something you are missing.



来源:https://stackoverflow.com/questions/17625414/attempt-to-compare-nil-with-number-in-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!