So I find myself facing a problem that I can't explain to myself, I started using HUMP to do my Classes and I get an error that I can't understand...
Here is the file that contains the class in question:
Code: Select all
Player = Class {
init = function(self, img, pos)
self.img = img
self.pos = pos
self.w, self.h = img:getDimensions()
self.ox, self.oy = self.w / 2, self.h / 2
end;
draw = function(self)
lg.draw(
self.img,
self.pos.x,
self.pos.y,
nil,
nil,
nil,
self.ox,
self.oy
)
end;
}
return Player
Code: Select all
Player.lua:8: attempt to index local 'img' (a nil value)
I "instantiate" it like this in another file:
Code: Select all
local path = ...
local Player = require(path.."/Player")
local player = Player()
local player_tex = lg.newImage(
"assets/images/player.png"
)
-- State definition --
local game = {}
function game:enter()
player:init(player_tex, Vec(display.size.w/2, display.size.h/2))
end
function game:update(dt)
--player:update(dt)
end
function game:draw()
player:draw()
end
return game
EDIT: I temporarily solved the problem by doing:
Code: Select all
if img ~= nil then
self.w, self.h = img:getDimensions()
self.ox, self.oy = self.w / 2, self.h / 2
end
Thanks in advance !