3

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?

gnat
  • 6,213
  • 108
  • 53
  • 73
Christian Eric Paran
  • 980
  • 6
  • 27
  • 49
  • 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 Jan 29 '12 at 09:41
  • 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 Jan 29 '12 at 09:46
  • 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 Jan 29 '12 at 09:52
  • 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 Jan 29 '12 at 09:59
  • 2
    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 Jan 29 '12 at 10:04
  • i tried cutting one of the sprite on a sprite sheet to get the width/height of that sprite but didn't work. – Christian Eric Paran Jan 29 '12 at 10:14
  • We can't really help you there without knowing the sprite sheet. If you had someone draw the spritesheet for you or you downloaded it somewhere, look for information there. As for dimensions, especially on classic consoles multiples of 16 were rather famous (e.g. 16x16, 32x32, 48x48 or 64x64). Don't use copyrighted works for things not being simple tests or internal placeholders. – Mario Jan 29 '12 at 10:18
  • your "sprite sheet" looks pretty much like job that is intended to be done with [TiledLayer API](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/javax/microedition/lcdui/game/TiledLayer.html) - did you consider it? – gnat Jan 30 '12 at 07:37

2 Answers2

3

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)

gnat
  • 6,213
  • 108
  • 53
  • 73
Daggio
  • 56
  • 4
1

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

123survesh
  • 561
  • 3
  • 13