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,
Classes with hump
- yetneverdone
- Party member
- Posts: 446
- Joined: Sat Sep 24, 2016 11:20 am
- Contact:
Re: Classes with hump
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
```
```
function foo:init(x, y)
self.x = x
self.y = y
end
```
Is the same as
```
foo.init = function(self, x, y)
--code
end
```
My GameDev Website
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Going Home:A Pixelated Horror Game
My Repositories!
Follow me lovingly!
Nga pala, pinoy ako.
Who is online
Users browsing this forum: Bing [Bot] and 229 guests