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

Actionscript 3/Starling Can't read embedded XML

我似乎无法解决这个嵌入式 XML 问题。我可以获得该对象,但是当我运行跟踪时... trace(myXML.children()); ... 我什么也得不到。我正在使用一些嵌入 XML 的纹理图集使用八哥。我不断收到错误消息:

ArgumentError: Texture cannot be null.

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

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

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. 首先纠正你的嵌入语法:

    [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)