简体   繁体   English

尝试索引全局“前”(nil值)

[英]Attempt to index global 'front' (a nil value)

I'm coding in Lua with Corona SDK and I get this error "Attempt to index global 'front' (a nil value)" in Line 75 我正在用Corona SDK在Lua中进行编码,并且在第75行中收到此错误“试图索引全局'front'(nil值)”

I'm doing this in game.lua, redirected from main.lua (this part works ok) .... Link is to pastebin, because stackoverflow messes the code! 我在game.lua中这样做,从main.lua重定向(这部分工作正常)....链接到pastebin,因为stackoverflow弄乱了代码!

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

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


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

    local bkg = display.newImage("bkg.png")

    local back = display.newImage("back.png")
    back.y=450
    back.speed = 1
    back:setReferencePoint(display.BottomLeftReferencePoint)

    local back1 = display.newImage("back.png")
    back1.y=450
    back1.x=2400
    back1.speed = 1
    back1:setReferencePoint(display.BottomLeftReferencePoint)

    local front = display.newImage("front1.png")
    front.y=470
    front.speed = 4
    front:setReferencePoint(display.BottomLeftReferencePoint)

    local front1 = display.newImage("front1.png")
    front1.y=470
    front1.x=2400
    front1.speed = 4
    front1:setReferencePoint(display.BottomLeftReferencePoint)

    local bird = display.newImage("bird.png")
    bird.y = 285; bird.x= 200
    physics.addBody(bird, "dynamic", 
                    {density=0.1, bounce=0.1, friction=0.2, radius=10})
end

function scrollHill(self,event)
    if self.x < -1800 then
        self.x = 1400 - self.speed * 2
    else
        self.x = self.x - self.speed
    end
end

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

function touchScreen(event)
   print("touch_ok")
   if event.phase =="began" then
    bird.enterFrame = activateBird
    Runtime:addEventListener("enterFrame", bird)
   end

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


function scene:enterScene(event)
    Runtime:addEventListener("touch", touchScreen)

    front.enterFrame = scrollHill
    Runtime:addEventListener("enterFrame", front)

    front1.enterFrame = scrollHill
    Runtime:addEventListener("enterFrame", front1)

    back.enterFrame = scrollHill
    Runtime:addEventListener("enterFrame", back)

    back1.enterFrame = scrollHill
    Runtime:addEventListener("enterFrame", back1)
end

function scene:exitScene(event)
end

function scene:destroyScene(event)
end

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

return scene

Most probably this is because the variable failed to initialize. 这很可能是因为变量未能初始化。

local front = display.newImage("front1.png")

Make sure the image file is located in the same directory as game.lua , and the file name is spelled correctly. 确保图像文件与game.lua位于同一目录中,并且文件名拼写正确。

You need to add forward declarations at the module's scope for your variables in createScene() so they are available to enterScene() . 您需要在模块范围内为createScene()变量添加前向声明 ,以便它们可以用于enterScene() Also, be sure to remove all your even listeners when you destroyScene. 此外,在销毁场景时,请确保删除所有偶数侦听器。

Ex: 例如:

-- Use a forward declaration here so `front` 
-- is in the module's scope
local front

function scene:createScene(event)
    -- ...
    -- Don't use `local` here because `front` was defined
    -- above.
    front = display.newImage("front1.png")
    front.y=470
    front.speed = 4
    front:setReferencePoint(display.BottomLeftReferencePoint)
    -- ...
end

function scene:enterScene(event)
    -- ...
    -- Since `front` is in the parent's scope, 
    -- we can access it here
    Runtime:addEventListener("enterFrame", front)
    -- ...
end

function scene:destroyScene(event)
    -- Be sure to remove your listeners here
    Runtime:removeEventListener("enterFrame", front)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM