views:

8788

answers:

5

Im currently using the following function to load an image, however i could not figure out a way to find the width of the loaded image, which i intend to use before placing the next image using the same function.

Note that q is a a variable (a number) which is used to load differant images.

=X i need help obtainning the loaded image width...

function LoadImage(q)
{
    var imageLoader:Loader = new Loader();
    var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
    imageLoader.load(image);
    addChild (imageLoader);
    imageLoader.x = 0 + CurrentXLength;
    imageLoader.y = 0;
    imageLoader.name = "image"+q;
    trace(imageLoader.x)
}
+1  A: 

To access the width you must do it within the function assigned to handle Event.COMPLETE.

"You will want an array containing the items you wish to load. You should probably load this data in with XML so it is dynamic and scalable. Once the xml data is loaded it should be assigned to an array in whatever fashion you like. The reason that we must use an array in this situation, rather then just using the XML object, which is essentially an array, is because you need the know an objects width so that you can base the next objects X position off of the last objects X position plus its WIDTH.

With XML it is common to use a for loop and just iterate through "x" amount of times. We do not want this, in this case. To obtain the "WIDTH" property of the loaded asset, it must be accessed from within the function assigned to fire when the loader fires Event.COMPLETE. Once the image has completed it will remove the item from the array, set a variable as to the lastX and lastWidth, and then get the next item in the array and start all over. Eventually the array is empty and the process is complete.

-Note: I will skip loading the XML and just inject the data into the array myself.

package
{
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.net.URLRequest;

    public class DocumentClass extends Sprite
    {
        private var _array:Array;
        private var _lastX:Number;
        private var _lastWidth:Number;

        public function DocumentClass():void
        {
            _array = new Array();
            //Add Items to an array however you wish.  I did it
            //this way to make it easier to read on this site.

            _array.push({name: "image1", path: "path/to/image1"});
            _array.push({name: "image2", path: "path/to/image2"});
            _array.push({name: "image3", path: "path/to/image3"});

            loadImage();
        }
        private function loadImage():void
        {
            if(_array.length > 0)
            {
                var loader:Loader = new Loader();
                addChild(loader);

                var request:URLRequest = new URLRequest(_array[0].path); //Loads first item in the array each time.
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
                loader.x = _lastX + _lastWidth;
                laoder.load(request);

                _lastX = loader.x; //We set after so we are ready for the next.

                _array.shift(); //Remove the first item from the array.
            }
        }
        function onImageLoaded(e:Event):void
        {
            _lastWidth = e.target.width;
            loadImage(); //Attempt to load another image if the array isn't empty.
        }
    }
}

I hope this helps, the code isn't tested, but the concept seems valid.

Brian Hodge

http://www.hodgedev.com

Brian Hodge
+7  A: 

You can't know the width of the bitmap until it's actually loaded:

function LoadImage(q)
{
    var imageLoader:Loader = new Loader();
    var image:URLRequest = new URLRequest("GalleryImages/Album1/"+q+".jpg");
    imageLoader.contentLoader.addEventListener(Event.COMPLETE, ImageLoaded);
    imageLoader.load(image);
    addChild (imageLoader);
    ...


private function ImageLoaded(e:Event):void
{
    var imageLoader:Loader = Loader(e.target.loader);
    var bm:Bitmap = Bitmap(imageLoader.content);
    var CurrentXLength = bm.width;
    ....

Alternativly this link might be helpful? Haven't tried it myself ...

Scott Evernden
Thanks, this helped, though I think the code is `content_loader.contentLoaderInfo.addEventListener(Event.CO` and not `imageLoader.contentLoader` ...
Yar
A: 

Thank God for you, BH! I've been beating my head against a wall trying to figure this one out. You saved my bacon.

curiojeff
A: 

Yeah I used scott's answer but it's worth noting that 'imageLoader.contentLoader' should be 'imageLoader.contentLoaderInfo' in the LoadImage() function. Solved the width prob for me-- thanks mang

A: 

Hi friend how are you? :) i have a questions how loaded image mc parent ???

private function ImageLoaded(e:Event):void
{
    var imageLoader:Loader = Loader(e.target.loader);
    var bm:Bitmap = Bitmap(imageLoader.content);
    var CurrentXLength = bm.width;
    //i try 
    e.target.parent.imgbg_mc

but this error ??

How ? Please answer me :)

i make friend :Dvar mc:MovieClip = MovieClip(imageLoader.parent.parent);