Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a SpriteSheet that have a lot of images in one in .png format, and the XML file with the all names of every sprite (x,y,width,height) like this:

<TextureAtlas imagePath="sheet.png">

<SubTexture name="box.png" x="0" y="864" width="70" height="70"/>

</TextureAtlas>

Im using Slick2D to make a BasicGame, SpriteSheet and XStream lib to manage the sprites, but i cant do, that load the image correctly. How I can do?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

You have to parse the xml file. It's not hard thing to do and this should get you started.

File file = new File("spritesheet.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
    .newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
NodeList subTextureNodeList = document.getElementsByTagName("SubTexture");

Now we have all the subtexture-data of spritesheet stored in subTextureList as an XML node. To read name,x,y, width and height of all images, do this:

for(int i = 0 ; i < subTextureNodeList.getLength(); i++){
    Element subTextureElement = (Element)subTextureNodeList.item(i);
    String name = subTextureElement.getAttribute("name");
    String x= subTextureElement.getAttribute("x");
    String y= subTextureElement.getAttribute("y");
    String width = subTextureElement.getAttribute("width");
    String height= subTextureElement.getAttribute("height");

    // DO SOMETHING WITH THIS DATA HERE
    // GET THE PICTURE FROM SHEET WITH THESE.
    }
}
share|improve this answer
    
WOW! thanks, its works fine ;). –  Nico Jul 23 '13 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.