2017-11-01 7 views
0

自己とローカルの値を理解するのは難しいです。 ここは私の小惑星です。私は作成し、他の機能を使用しようとしていたが、正しく機能していない、内部の小惑星を作成した。luaエラー:フィールド 'frames'(nil値)のインデックスを作成しようとしましたが(自己および致命的なエラー)

-- 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)は、問題を解決するために使用してください。 私は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 

エンド

答えて

0

コードの残りの部分を見ることなく、私は推測することができます。試してみてください:

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

はい、自己変更後に動作します。ありがとう –

関連する問題