XMLPackedSheet.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:trashjam2017 作者:
/**
 * Create a new XML packed sheet from the XML output by the slick tool
 * 
 * @param imageRef The reference to the image
 * @param xmlRef The reference to the XML
 * @throws SlickException Indicates a failure to parse the XML or read the image
 */
public XMLPackedSheet(String imageRef, String xmlRef) throws SlickException
{
    image = new Image(imageRef, false, Image.FILTER_NEAREST);

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(ResourceLoader.getResourceAsStream(xmlRef));

        NodeList list = doc.getElementsByTagName("sprite");
        for (int i=0;i<list.getLength();i++) {
            Element element = (Element) list.item(i);

            String name = element.getAttribute("name");
            int x = Integer.parseInt(element.getAttribute("x"));
            int y = Integer.parseInt(element.getAttribute("y"));
            int width = Integer.parseInt(element.getAttribute("width"));
            int height = Integer.parseInt(element.getAttribute("height"));

            sprites.put(name, image.getSubImage(x,y,width,height));
        }
    } catch (Exception e) {
        throw new SlickException("Failed to parse sprite sheet XML", e);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号