【问题标题】:lua error: attempt to index field 'frames'(a nil value)(but self and lacal errors)lua 错误:尝试索引字段“帧”(一个零值)(但自身和本地错误)
【发布时间】:2018-04-13 06:33:41
【问题描述】:

我很难理解自我和本地价值。 这是我的 asteroid.lua。我在 :create 中创建了本地小行星,并试图使用它的其他功能,但它无法正常工作。

-- Class Declaration

Asteroid = {}
Asteroid.__index = Asteroid


function Asteroid:create()
local asteroid = {}
setmetatable(asteroid, Asteroid)

-- Animation Data
asteroid.frames = {}
asteroid.currentFrame = 1
asteroid.frameDuration = 0.04  -- 0.016
asteroid.frameTimeRemaining = asteroid.frameDuration
asteroid.x = 0
asteroid.y = 0

return asteroid
end

function Asteroid:init()

-- Use a loop to load a bunch of files!
for index= 0, 15 do
    -- Use logic to build the filename...
    file = 'art/large/a100'

    -- Take into account the extra 0
    if index < 10 then
        file = file .. '0'
    end

    -- Add the file number and then .png
    file = file .. tostring(index) .. '.png'

    -- Load the file...  (we'll use lua's 1 index to be kind)
    self.frames[index + 1] = love.graphics.newImage(file)

    if self.frames[index + 1] ~= nil then
        print('Loaded frame ' .. tostring(index + 1))
    end
end

-- Set the velocity randomly!
self.velocity = {}
self.velocity.x = math.random(-76.0, 76.0)
self.velocity.y = math.random(-76.0, 76.0)

end

Asteroid.updateAnimation = function(deltaTime)
-- Catch variable into 'easy-to-type' one
local ftr = Asteroid.frameTimeRemaining

这部分(本地 ftr)是我的问题:尝试索引 NIL 值。 我尝试了不同的方法,比如 self.frameTimeRemaing,但我想不通。 有谁知道我该如何解决这个本地问题?

-- Subtract time
ftr = ftr - deltaTime

-- If the frame is over...
if ftr < 0 then
    Asteroid.nextFrame()
    Asteroid.frameTimeRemaining = Asteroid.frameDuration
else
    Asteroid.frameTimeRemaining = ftr
end

结束

【问题讨论】:

    标签: lua


    【解决方案1】:

    没有看到其余的代码,我只能猜测。试试这个:

    function Asteroid:updateAnimation(deltaTime)
    -- use self instead of Asteroid in this function
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2014-05-27
      • 1970-01-01
      • 2021-05-15
      • 2016-09-19
      相关资源
      最近更新 更多