Page 2 of 3
Re: class problem.
Posted: Thu Jan 27, 2011 4:38 pm
by TheBreadCat
leiradel wrote:Ok, here's the code that I'm running without errors:
Code: Select all
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.
but when i call this function:
Code: Select all
function cCake:MoveDown()
self.y = self.y+1
end
by doing this:
i get an error.
Re: class problem.
Posted: Thu Jan 27, 2011 4:45 pm
by bartbes
Because oCake.y doesn't exist?
Re: class problem.
Posted: Thu Jan 27, 2011 4:51 pm
by TheBreadCat
bartbes wrote:Because oCake.y doesn't exist?
Code: Select all
function cCake:init()
self.x = 0
self.y = 0
self.hspeed = 0
self.vspeed = 0
end
Re: class problem.
Posted: Thu Jan 27, 2011 5:10 pm
by TheBreadCat
it does it must be something else.
Re: class problem.
Posted: Thu Jan 27, 2011 5:11 pm
by kikito
TheBreadCat wrote:bartbes wrote:Because oCake.y doesn't exist?
Code: Select all
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.
Re: class problem.
Posted: Thu Jan 27, 2011 5:17 pm
by TheBreadCat
TheBreadCat wrote:
still does not work just gets this:
Code: Select all
attempt to perform arithmetic on field 'x'(a nil value)
done before.
just forgot to repost it.
and the __init is changed to init beouse im using
http://love2d.org/wiki/Simple_Educative_Class_System.
Re: class problem.
Posted: Thu Jan 27, 2011 5:41 pm
by leiradel
Can you post a .love file we can use to reproduce the problem?
Re: class problem.
Posted: Thu Jan 27, 2011 5:49 pm
by Robin
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.
Re: class problem.
Posted: Fri Jan 28, 2011 3:09 pm
by TheBreadCat
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()
Code: Select all
function class:new(...)
local c = {}
c.__baseclass = self
setmetatable(c, getmetatable(self))
if c.init then <--------
c:init(...) <---------
end
return c
end
Re: class problem.
Posted: Fri Jan 28, 2011 3:34 pm
by leiradel
TheBreadCat wrote:are you even listening?
What about you, are you listening? I've asked if you could post a .love file that reproduces the problem and you didn't even answer.
You won't get much help with this kind of attitude.