0
votes

I'm trouble to understand self and local value. Here is my asteroid.lua. I created local asteroid inside :create and was trying to use it other function but it is not working correctly.

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

THIS PART(local ftr) IS MY PROBLEM: ATTEMPT TO INDEX A NIL VALUE. I tried different ways like self.frameTimeRemaing but I couldn't figure out. Is anyone know how I can fix this local?

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

end

1

1 Answers

0
votes

Without seeing the rest of the code, I can only guess. Try this:

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