Search code examples
xmlactionscript-3texturesstarling-framework

Actionscript 3/Starling Can't read embedded XML


I can't seem to get this embedded XML issue solved. I can get the object but when I run a trace... trace(myXML.children()); ... I get nothing. I am using some embedded XML for a texture atlas using starling. I keep getting an error:

ArgumentError: Texture cannot be null.

Can you guys help? See code below.

Calling this form my menu screen...

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];
        }
    }
}

Solution

    1. First of all correct your embed syntax:

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

      should be:

      [Embed(source="assets/atlas.xml", mimeType="application/octet-stream")]
      
    2. And second, mimeType="application/octet-stream" means that you are about to embed file as a raw byte array, so with a new operator you will get this bytes rather than string, but you always can convert it to 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)