Page 2 of 2

Re: Multiple questions : math.random, objects, events ...

Posted: Tue Apr 15, 2014 11:33 pm
by Robin
Could you please upload a .love, because helping you like this is very hard. What you're doing now is like taking a picture of a haystack and asking us to please find the needle lost in there, instead of giving us the haystack to look through ourselves.

You'll also need to change

Code: Select all

    self:speed=speed
to

Code: Select all

    self.speed=speed

Re: Multiple questions : math.random, objects, events ...

Posted: Wed Apr 16, 2014 12:21 am
by moikmellah
You need ':' in both places.

In Lua, declaring a function like so:

Code: Select all

function Thing:doStuff(x, y)
  -- do stuff here
end
..is the same as declaring it this way:

Code: Select all

function Thing.doStuff(self, x, y)
  -- do stuff here
end
The ':' tells Lua that the function should implicitly accept a first argument stored in a local variable called 'self'. Likewise, calling a function like so:

Code: Select all

myThing:doStuff(x, y)
..is the same as calling it as follows:

Code: Select all

myThing.doStuff(myThing, x, y)
When calling a member variable as a function, using a ':' tells Lua that it should implicitly pass the table/object as the first argument.

So, for 'self' to exist and do what you're expecting, you should use ':' both when declaring and calling the function.

(see PIL ch16 for a better explanation)

Re: Multiple questions : math.random, objects, events ...

Posted: Wed Apr 16, 2014 2:48 pm
by Roland_Yonaba
Actually, when reading how this thread is going, I feel very bad, just because I am the one who pointed you to some class libraries.
Judging from the questions you are asking and the code typos you are making, it seems to me the problem is basic understanding of OOP (objects, classes, etc).
I would highly suggest something: if you feel pretty comfortable right now with tables, try taking a close look at the examples Kikito wrote on MiddleClass's github repository. And take a look at them very closely, in order to understand what is going on. How classes are defined, how objects are defined, what is the real meaning of calling a property/method via a dot vs via a colon, etc. And we can rpovide some help with that. I am pretty sure after that, it will now be much more easier the use any class library, or write your own class framework.

Here you are some additional lectures, feel free to check them out. It will help, tremendously, in my humble opinion.
And please, feel free to ask any questions in case it anything is unclear.