Hi,
Having the following code to create a new class:
Player = Core.class(Sprite) -- create your own player class
function Player:init()
-- do the initialization of Player instance
self.health = 100
self.speed = 3
end
function Player:walk()
-- walk logic
end
function Player:jump()
-- jump logic
end
stage:addChild(Player.new())
My questions are, how can I access to the health and speed instance variables from walk and jump methods? also, how can I invoke walk method from jump method. I'm looking for a "this" variable that allows me to access the methods o properties of its own instance.
Also, I would like to know how to call the parent construct. I wanted to inherit Bitmap and I didn't know how to pass to it the texture.
Regards,
-----
Isaac Zepeda
Comments
Have a look at this code, whic creates an instance called "newPlayer". You can also have "newPlayer2", "newPlayer3", etc, each of which will be separate instances and you can allocate health and speed to each of these instances.
In the init function, you can do function Player:init(speed), so when you do newPlayer.new(50), you allocate 50 as the speed for that instance.
The "self." is the equivalent of "this."
A nice way to remember is to only access methods (class functions) via the : operator and fields (class variables) using the . operator.
"self" is (if my understanding of Lua is correct) is basically a reference to the table which is created as part of the Class.new() command - I still haven't managed to work out exactly what happens with the tables when an object instance is created (something with metatables I suspect) but it seems to work (and the Gideros approach is the cleanest OOP implementation I've seen) so for now I'm just closing my eyes and trying not to worry about it.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
If you could inherit from your own classes this might be an issue but you can't. Actually having a limited hierarchy in a lot of ways is a benefit, you can actually create complex objects using a component "plug-in" style system, where functionality comes from smaller objects being (re)used rather than trying to have an all-encompassing object tree structure.
A quick google on "component game design" should shed some light on things.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Likes: karnakgames
Base:myCall()
Character:myCall()
Character:anotherCall()
If Base calls myCall it will call Base:myCall()
If Character calls myCall it will call Character:myCall()
If Base calls anotherCall it will call Character:anotherCall()
To be honest I wasn't aware that you *could* inherit in this way - it might be useful to know for future use, thanks for sharing.
At the end of the day the best way to prove or dis-prove anything is via experimentation and putting some well placed print() statements in the code will always tell you which functions are being called (and when)
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Likes: techdojo
Well done!
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
For example, if class A (in classA.lua) inherits from class B (in classB.lua) and you're getting a message like "attempt to index global 'B' (a nil value)", select "classA.lua" and add a dependency on classB.lua.
--ND
Here's an example:
If you try to use "self:method()", you'll be recursively calling "B:method()"
»Gideros Illustrator« - [svg|xml] scene designer using Adobe Illustrator®™ Within one line of code!
Likes: MikeHart, atilim
My apps: http://www.yummyyellow.com
I think the thing with Lua is that it can do a lot of powerful things that are a lot of work in other languages but are easy and can be hacked together in Lua because functions are 1st class values, everything is a table, and if you know how to do it to one thing you use the same steps for everything else. It even has single-threaded threading with coroutines and they are so easy to use compared to multithreaded threading. Everything is just so easy and I really appreciate simplicity.
I started learning Lua because I thought it would be good enough for my needs. Now I'm thinking it is the best choice. Gideros is also a pretty good fit. And I have to admit that being able to develop on my iPad and run it on my iPhone with Dropbox as the intermediate step is a major feature for me and I'm not sure I would be able to do that with any other development product without jailbreaking my phone, which I do not want to do.
I've also learned a lot or new programming concepts since I've been learning Lua. I sort of knew what "self" (or "this") was but after learning how Lua does OO I now feel like I have a much better understanding about how OO really works.
My apps: http://www.yummyyellow.com
For example I have some DB operations, and want to close the DB before killing the class.
---------------------------------------
best regards
Likes: Cyberience
i could do A.init(self) probably(?), but what if my function has some parameter(s) p?
then would A.init(self,p) work, or how?
thanks for the help
Fragmenter - animated loop machine and IKONOMIKON - the memory game
And since init would return reference to self instance, you would not get anything from it.
But all what you described is true A.init(self) and A.init(self,p) should work as you expect, only no point to use on init. Try wrapping needed functionality inside other A method and call it instead.
Basically don't mess with init
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
local variable = value places a local var within the function, inside a class, it is still within the function,
if I do a self:variable = value, it is available across the class, but also externally, is there another operator that would allow private variable across all functions of the class?
---------------------------------------
Fragmenter - animated loop machine and IKONOMIKON - the memory game
As in there is no concept of private method and private variable in lua.
But from a good practice stand of point, usually variable and method/functions that are meant to be used internally, are named with _ prefix: