Pygame Rect in Love?

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
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Pygame Rect in Love?

Post 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 :3 Also, this be löve, so it can be implemented differently, better even.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
59 Byte
Prole
Posts: 8
Joined: Mon Mar 14, 2016 3:52 am

Re: Pygame Rect in Love?

Post by 59 Byte »

I've written my own thing based on MadByte, Kingdaro, and zorg's examples. Here it is:
Rect.love
(1.05 KiB) Downloaded 159 times
Thanks everyone!
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Pygame Rect in Love?

Post 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})
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Pygame Rect in Love?

Post 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 :3

Code: Select all

return Rect.new
Though my constructors are usually local, and if needed, i create a copy method that accesses the constructor.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Pygame Rect in Love?

Post 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
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Pygame Rect in Love?

Post 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. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
bobismijnnaam
Prole
Posts: 18
Joined: Thu Mar 17, 2016 5:18 pm
Location: Netherlands
Contact:

Re: Pygame Rect in Love?

Post 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
taart
lc = love.timer -- love.chrono :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests