Classes with hump

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
LordGaga
Prole
Posts: 1
Joined: Sun Oct 20, 2019 3:21 pm

Classes with hump

Post by LordGaga »

Hello,

I'm trying to make a class inherit the properties of another class using the Hump library. So far, it works very well. However, when I try to create my class in this way, in doesn't work anymore:

""
Item=Class{

init=function(x,y,size)
self.x=x
self.y=y
self.size=size

self.dx=dx
self.dy=dy
self.dx2=dx2
self.dy2=dy2
end;

update=function(dt)
self.dx=self.dx+self.dx2
self.x=self.x+self.dx*dt
self.dy=self.dy+self.dy2
self.y=self.y+self.dy*dt
end;

coliide=function(ball)
return math.sqrt((self.x-ball.x)^2+(self.y-ball.y)^2)<self.size
end;

reset=function()
self.x=love.graphics.getWidth()/2
self.y=love.graphics.getHeight()/2
self.dy=0
self.dx=0
self.dy2=0
self.dx2=0
end

}
""

The error message tells me that the 'self' is a nill value. Can someone help me and tell me what's wrong in this code please?

Thank you and regards,
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Classes with hump

Post by yetneverdone »

You have to put "self" as the first parameter to the class methods. Please note that in lua:

```
function foo:init(x, y)
self.x = x
self.y = y
end
```

Is the same as

```
foo.init = function(self, x, y)
--code
end
```
Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests