How to get the size of sprite in a sprite "Sheet"?

5.8k views Asked by At

my problem is getting the Sprite in a Sprite "Sheet".

    public class Blocks extends Sprite{
        public Blocks() throws IOException
            {
                super(Image.createImage("/blockSprite.png"),20,30);
            }
    }        

"the blockSprite.png contains images of Sprites, many sprites in 1 image"

The problem here is using this line of code, how I am supposed to get the Sprite size/dimension in the image?

Sprite Sheet:
Width - 162
Height - 280

I want to know the width/height of the sprites in the sheet? and how to determine the width/height of the sprites in a Sprite Sheet?

2

There are 2 answers

0
Daggio On BEST ANSWER

You do know how many sprites are there in each row and column right? then just divide the width with how many sprites are there in a column and divide the height with how many sprites are there in a row.

(I may get the column and row wrong, I'm not native English, but you should just switch the row/column if the first try didn't work out)

0
123survesh On

One way is to use an image editing software (say GIMP, its free). What you have to do is,

  • Open the image in GIMP
    • Click on the Rectangle selection tool (or press r), your mouse will turn into a cross hair
    • Now when you click on the image, in the tool box, under tool options you will see the X-Y position of the point you clicked
    • Cool, now draw a rectangle around the sprite that you want. Zoom in before you do this, so that you are actually selecting the whole sprite.
    • Now you will get both the X-Y coordinates of the top-left corner of the rectangle that you drew and the height and width of the image.

After that, just use https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#Slicing.

Have fun!!