function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = function (c, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end})
end
player = class()
function player:__init()
self.x = 320
self.y = 240
self.hspeed = 0
self.vspeed = 0
self.hp = 0
end
function player:update()
self.x = self.x + self.hspeed
self.y = self.y + self.vspeed
self.vspeed = 0
self.hspeed = 0
end
p = player()
function love.update(dt)
p:update()
end
I've created a test folder in the same folder where love.exe lives, and put the above code in a main.lua file inside test. I'm then able to run it fine by either dragging test over love.exe or via the command line: love test.
function cCake:init()
self.x = 0
self.y = 0
self.hspeed = 0
self.vspeed = 0
end
Wasn't your constructor method called __init (with underscores)?
Also, for future posts: If you say "I get an error" it's very difficult to know what happens. It's better saying "I get the following error:" and paste the error message after it on a [ code ] [ /code ] section.
Robin wrote:Also, you're creating a global called "instance". This does not seem to have been fixed by anyone. Not the most important of your issues, but still.
init() is not called on your class system, but __init() is, which is why it doesn't work. Change either one in the other, and it should work.
are you even listening?
i changed libary os now its init()
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then <--------
c:init(...) <---------
end
return c
end