0

嘿伙计们,我即将完成我的游戏我是一名独立开发者,我正在尝试在 corona sdk 中完成我的应用程序,但我一直收到这个烦人的错误消息。得到的消息是“尝试将 nil 与数字 restart.lua 9 进行比较”我不知道如何解决这个问题,我尝试了所有方法,但没有任何效果。当我运行我的代码并且我第一次死亡并重新启动游戏时它工作正常但第二次玩家死亡并且我尝试重新启动游戏我收到此错误消息。这是代码

好的,我想清楚这一点,所以我将在游戏 2 文件中发布我的漏洞代码,以便你们了解我在做什么。

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

-- 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 

下面是粗体的代码是给出问题的代码希望你们能告诉我什么是错的..

***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
4

1 回答 1

1

显然,self.x 不存在。确保在第 9 行进行比较之前将其初始化。我认为你很幸运,这些错误通常很容易解决。

如果您仍然无法弄清楚,请发布您首次初始化 self.x 的位置,这可能是您缺少的东西。

于 2013-07-12T23:30:55.233 回答