What's the best way to read a sprite sheet in Java?

I am writing a basic script for my own entertainment and am better at Java 2d API. I currently use a large number of separate .png files with transparent backgrounds to represent the different sprites and different animation frames that I need. Most real world game development projects seem to use "sprite sheets," which contain multiple sprites or animation frames in a single file. Also, instead of using native image transparency support, people often assign an arbitrary color that does not appear in the Sprite Palette to be a transparent color. How do I manage a file like this programmatically?

  • how do you know where one sprite starts and the next starts.
  • how do you deal with transparency.

There may be other factors that I haven't thought of, so I can add to the list above as I think about things or how people make suggestions (please do so in the comments).

0


source to share


5 answers


I am currently using XML files created by a simple sprite editor that store a sprite as a collection of (optionally animated) poses, which in turn are a collection of frames or cells. Frames store information per frame such as the x and y offsets of the frame in the sheet, the width and height of the cell, and any transformation (resize / rotate / tint / etc). Poses store individual frames and animation information (such as speed) and the pose name so that they can be easily identified in the program (hero.pose = sprite.pose ["stand_right"]). The sprites serve as the document root to hold multiple poses such as a pose for each directional direction.

, , (, 32 , 32 * 2). (, sprite_name_32x64.png) , . , , .



I use the alpha and transparency information stored in PNG images directly, so I don't have to worry about storing it elsewhere, although other approaches are to pick a fixed value for the sprite and store somewhere, use the leftmost pixel in if you know it is always empty use a specific palette entry if you are using these sprite masks or whatever you have.

+1


source


some info including java Tilestudio demo



0


source


Make your sprite sheet knowing the size and quantity of each sequence.

Take a buffer image of your sheet and use something like this:

currentframe=spritesheet.getSubimage(x, y, w, h); 

      

Your x and y will change depending on the frame you are in. Keep the width and height the same to make your life easier.

Forget about trying to save your entire game on one sheet. These are nuts and difficult to handle. Use a new png for each animation sequence. If you are a space preserver anal, create only moving right animations and simply flip the live buffer to move left.

Java will read png files with alpha, so don't worry about transparency color. Draw everything in png. Use Photoshop or Gimp.

Google search for java image TYPE_INT_ARGB

0


source


Not about java, but usually you can make all sprites the same size. This way you will be able to create your sprites in your game (or application) with easy-to-loop loops.

But for sprites of different sizes, there might be a problem for the spritesheet size (it might be larger than expected). Therefore, you must define xml or json file for the sprite to find your sprite images in your code. You can use the Sprite List editors (there are many, I use Sprite Master ) for a quick and easy way to create a sprite sheet and coordinate Data.

0


source


Ok, since most of them are common, these details are appropriate for the developer. Typically you have a file that starts with header information containing details of height / width and encoding, transparency, etc.

Lots of things in one file because it is very expensive to open / read multiple files compared to opening / reading a single file. Many game engines use 0 compression zip or ziplike files to handle a single file as a file system.

-1


source







All Articles