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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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
Help us help you: attach a .love.
User avatar
moikmellah
Prole
Posts: 12
Joined: Fri Jan 31, 2014 8:31 pm
Location: USA
Contact:

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

Post 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)
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests