R – Actionscript embed an array


I want to embed some images at compilation time so I end up with just a single swf.
They need to be inside of an array as I need to modify them programatically and they can be 100s of images. Cant use flex as I want to keep the whole function in actionscript (aka make files smaller)

I found how to do it in flex:

<mx:Array id="test">
   <mx:Image id="image0" source="@Embed(source='../../../lib/Images033,jpg')" />
   <mx:Image id="image1" source="@Embed(source='../../../lib/Images034,jpg')" /> 
   <mx:Image id="image2" source="@Embed(source='../../../lib/Images035,jpg')" />
   <mx:Image id="image3" source="@Embed(source='../../../lib/Images036,jpg')" />
addChild(test[1] as something);

So does anyone know how to do the above but just in Actionscript?

Many thanks.

Best Solution

Well, one way or another, you're going to need an Embed statement for every thing you want to embed, so you won't really be able to get around that. But if you'd prefer to handle everything in script, you could do something like this (it's an AIR application, but everything between the WindowedApplication tags should work within a plain ol' Flex app):

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="onInitialize()">


            import mx.controls.Image;

            private var images:Array;
            private const IMAGE_COUNT:uint = 5;

            private var Image0:Class;

            private var Image1:Class;

            private var Image2:Class;

            private var Image3:Class;

            private var Image4:Class;

            private function onInitialize():void
                images = new Array(IMAGE_COUNT);

                // Populate your array with Class references to embedded imagery
                for (var i:int = 0; i < IMAGE_COUNT; i++)
                    images[i] = this["Image" + i];

            override protected function createChildren():void

                // Add your children to the display list
                for (var i:int = 0; i < IMAGE_COUNT; i++)
                    var img:Image = new Image();
                    img.source = images[i];




So essentially what you're doing, after making sure your images are all embedded at compile time and named according to some numeric scheme (in this case, just appended with an index), is filling your array with Class references, then instantiating and adding them to the display list during the createChildren() phase of the component lifecycle.

There's some esoteric stuff going on here, so if you don't quite understand everything, feel free to comment back, and I'll keep an eye on things. But this is tested code and should work pretty well for ya, given how you've explained your requirements so far.

Related Question