Hi everyone,
why making classes for getting instances from them is this much hard in lua and gideros?
I knew a method to simulate classes in lua. it was making a table (for example "boat") with the fields and functions we want and then making a methode like this:
function boat:new()
return self
end |
and whenever we wanted to use the boat object we simply wrote:
but in this case when I write
local boat={
side="left",
pic=Bitmap.new(Texture.new("boat.png")),
}
function boat:GoX()
if self.pic:getX()<1150 then
self.pic:setX(self.pic:getX()+10)
self:move()
else
self.side="right"
end
end |
and when I use "boat:GoX()" function I get this error
gamesCore.lua:50: attempt to index local 'self' (a nil value)
stack traceback:
gamesCore.lua:50: in function 'func'
[string "compatibility.lua"]:32: in function <[string "compatibility.lua"]:31> |
so how can I use "self" keyword to be able to use a object's fields inside it's functions?
If anyone could explain what is the easiest and efficient way to simulate java-like classes in lua, it would a great help for me.
Thanks in advance.
Comments
Likes: aimoi
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: aimoi
But even with the "core.class" things I still have problems using self keyword.
I wrote:
I was wondering about making a metatable for all my boat objects and then set their metatables to that. Is that a better approach that using Core.class?
Likes: aimoi
Likes: aimoi
Likes: aimoi
well, first after changing the things you said, I still had the
But seems like the problem has not been solved yet. I need to make delay there but with this function I get a nil value in 'self' when I call the GoX function of a specific boat. I think it's because the timer function is not sending the object inside the function the way when using ':' it does. So is there a solution for this too?
Likes: aimoi