Actionscript 3 / Starling无法读取嵌入式XML

时间:2015-01-02 01:04:44

标签: xml actionscript-3 textures starling-framework

我似乎无法解决这个嵌入式XML问题。我可以得到这个对象,但是当我跑出一条痕迹...... trace(myXML.children()); ......我什么都没得到。我正在使用一些嵌入式XML作为使用starling的纹理图集。我一直收到错误:

  

ArgumentError:纹理不能为空。

你能帮忙吗?请参阅下面的代码。

在我的菜单屏幕中调用此表格...

playButton = new Button(Assets.getAtlas().getTexture("play"));

Assets.as

package  
{
    import flash.display.Bitmap;
    import flash.utils.Dictionary;

    import starling.textures.Texture;
    import starling.textures.TextureAtlas;


    public class Assets
    {
        private static var gameTextures:Dictionary = new Dictionary();
        public static var gameTextureAtlas:TextureAtlas;
        [Embed(source="assets/sky.png")]
        private static const sky:Class;

        [Embed(source="assets/atlas.png")]
        public static const atlasTexture:Class;

        [Embed(source="assets/atlas.xml"), mimeType="application/octet-stream")]
        private static var atlasXML:Class;



        public static function getAtlas():TextureAtlas
        {
            if(gameTextureAtlas == null){
                var xml:XML = XML(new atlasXML());
                var texture:Texture = getTexture("atlasTexture");
                gameTextureAtlas = new TextureAtlas(texture, xml);

            }

            return gameTextureAtlas;

        }

        public static function getTexture(name:String):Texture
        {

            if(gameTextures[name] == undefined){
                var bitmap:Bitmap = new Assets[name]();
                gameTextures[name] = Texture.fromBitmap(bitmap);
            }

            return gameTextures[name];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 首先纠正您的嵌入语法:

    [Embed(source="assets/atlas.xml"), mimeType="application/octet-stream")]
    

    应该是:

    [Embed(source="assets/atlas.xml", mimeType="application/octet-stream")]
    
  2. 第二,mimeType="application/octet-stream"意味着您要将文件嵌入为原始字节数组,因此使用new运算符将获得此字节而不是字符串,但您始终可以将其转换为XML

        var bytes:ByteArray = new atlasXMLBytes();
        trace( "bytes.length = ", bytes.length );
    
        //read the whole file as UTF string and convert it to the XML
        var xml:XML = new XML( bytes.readUTFBytes( bytes.bytesAvailable ) );
        trace("xml= "+xml)