Page 2 of 2
Re: Pygame Rect in Love?
Posted: Mon Mar 14, 2016 11:47 am
by zorg
T-Bone wrote:I just realized something. If I change "bottom" to a larger value, should that mean that the rectangle moves down, or that it becomes taller? That's not obvious to me. Maybe functions like "moveToBottom" and "expandToBottom" are better for clarity.
Pygame rect reference wrote:
Assigning to size, width or height changes the dimensions of the rectangle; all other assignments move the rectangle without resizing it. Notice that some attributes are integers and others are pairs of integers.
I personally find this less than appealing design, but to each their own, i guess
Also, this be löve, so it can be implemented differently, better even.
Re: Pygame Rect in Love?
Posted: Sun Apr 03, 2016 11:26 pm
by 59 Byte
I've written my own thing based on MadByte, Kingdaro, and zorg's examples. Here it is:
Thanks everyone!
Re: Pygame Rect in Love?
Posted: Mon Apr 04, 2016 5:06 am
by MadByte
One thing, you declared your constructor global which shouldn't be the case. Instead try this.
Code: Select all
function Rect:new(x,y,w,h)
local self = setmetatable({}, Rect)
self.x = x
self.y = y
self.width = w
self.height = h
return self
end
-- and at the end of the file return this
return setmetatable({}, {__call=Rect.new})
Re: Pygame Rect in Love?
Posted: Mon Apr 04, 2016 5:18 am
by zorg
MadByte wrote:Code: Select all
-- and at the end of the file return this
return setmetatable({}, {__call=Rect.new})
I always returned just the constructor function in cases like this though, no need for a table to be called
Though my constructors are usually local, and if needed, i create a copy method that accesses the constructor.
Re: Pygame Rect in Love?
Posted: Mon Apr 04, 2016 5:38 am
by MadByte
You're right.
So I guess it would be better to do
Code: Select all
function Rect.new(x, y, w, h)
...
end
...
return Rect.new
or
Code: Select all
local function newRect(x, y, w, h)
...
end
...
return newRect
I don't like the second one that much but it works.
There was a reason why I used __call in addition to some other stuff sometimes in the past, but I don't know it anymore :S
Re: Pygame Rect in Love?
Posted: Mon Apr 04, 2016 6:35 am
by zorg
If you wanted to return a table full of "class" functions, then that would be a solution, calling the table calling the constructor.
Re: Pygame Rect in Love?
Posted: Mon Apr 04, 2016 11:26 am
by bobismijnnaam
I actually implemented the bounding box with the .left or .bottom member thing, if people are interested they can see it here:
http://pastebin.com/SG8PgtfM