2
votes

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
Could you reword your question? It's not really clear what you're looking for. To me it looks like you know the width and height of he texture/sprite sheet as well as the width/height of the sprites? Are you looking for the position of a frame? or the number of frames in the sheet?Mario
sorry about that. well, 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.Christian Eric Paran
Looked up the Sprite class and the constructor used here is obviously Sprite(Image image, int frameWidth, int frameHeight), which suggests that the second and third parameter are your sprite size? That's the part I'm confused about. If that isn't what you're looking for, try to add some sample code to demonstrate where you're missing some value or where you'd like to put it. Please consider that you have to either name the number of rows/columns or the width/height of frames. You have define one or the other.Mario
the sprite size there is just a incorrect size of the sprite. i have sample projects here that uses the same Sprite class but not the same Sprite sheet. example: super(Image.createImage("/kirby.png"),32,48); . i want to name the width/height of frames.Christian Eric Paran
Well, that's something you have to base on your sprite sheet images. You can't calculate it "out of thin air". You'll most likely have to hard code the values or define them in some file.Mario

2 Answers

1
votes

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!!

0
votes

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)