In the tutorial in the love2d official tutorials page, https://love2d.org/wiki/Tutorial:Animation, there's a part I don't understand. The for loop conditionals that go like this:
for y = 0, image:getHeight() - height, height do
Why are we doing this? I read the explanation on the tutorial page but sadly can't make sense of it. I would highly appreciate if anyone could explain this to me.
Dividing spritesheets into quads
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Dividing spritesheets into quads
The subimages inside the image are each width x height. If the image's width or height is not a multiple of the subimage's width or height, and you go past the image height minus the subimage height, you would be trying to take a quad that would go past the image border.
Let's take a simplified example. Say we have a 10x10 image and the subimages are 4x4. That means that you can take 4 quads: one at (0, 0) which extends to (3, 3) inclusive, one at (4, 0) which extends to (7, 3) inclusive, one at (0, 4) which extends to (3, 7) inclusive, and one at (4, 4) which extends to (7, 7) inclusive.
If you loop, say, y from 0 to image:getHeight()-1, you would be looping from 0 to 9 in steps of 4. That would loop over the numbers 0, 4 and 8. When y is 8, you would be trying to take a 4x4 quad at (8, 0), extending up to (11, 3). But the image is only 10 pixels high.
So, you need to loop up to the height of the image minus the height of the subimage. That guarantees that you will always have enough height for each quad in the last row. Similarly for columns.
Let's take a simplified example. Say we have a 10x10 image and the subimages are 4x4. That means that you can take 4 quads: one at (0, 0) which extends to (3, 3) inclusive, one at (4, 0) which extends to (7, 3) inclusive, one at (0, 4) which extends to (3, 7) inclusive, and one at (4, 4) which extends to (7, 7) inclusive.
If you loop, say, y from 0 to image:getHeight()-1, you would be looping from 0 to 9 in steps of 4. That would loop over the numbers 0, 4 and 8. When y is 8, you would be trying to take a 4x4 quad at (8, 0), extending up to (11, 3). But the image is only 10 pixels high.
So, you need to loop up to the height of the image minus the height of the subimage. That guarantees that you will always have enough height for each quad in the last row. Similarly for columns.
Re: Dividing spritesheets into quads
I'd suggest you use a dedicated software for this instead of managing all of that by hand.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 5 guests