A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: load in center

  1. #1
    Junior Member
    Join Date
    Feb 2009
    Posts
    17

    load in center

    I'm using this AS to load multiple external images into mc instances on a stage.

    //1
    function loadImage1 ( value : String ) : void
    {
    loader1 = new Loader();
    loader1.load(new URLRequest(value));
    loader1.contentLoaderInfo.addEventListener(Event.C OMPLETE, loaderComplete1);
    }

    function loaderComplete1( e: Event )
    {
    loader1.contentLoaderInfo.removeEventListener(Even t.COMPLETE, loaderComplete1);
    mc1.addChild(loader1);
    }

    loadImage1 ( '2kro.jpg' );





    The external images always load from the upper left corner down, how can you make the image load from the center of the instance on stage?

  2. #2
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    I found this and doubt it will work for me since I have 5 mc instances on stage I'm using separate loaders for, so the stage wouldnt be a good reference point.

    movieclipname._y = Stage.height / 2 - movieclipname._height / 2;
    movieclipname._x = Stage.width/ 2 - movieclipname._width/ 2;

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    14
    Hey there mikeo1313,

    The last bit of script you posted was in AS2... so not really what you need since your first post uses AS3.

    What you can do is use a simple method to load all images in the correct order and then add them into whichever movieclips you want, for example...

    1. Create an array of the movieclips on stage
    2. Create an array of the necessary image urls... although XML would be better
    3. Create a single method to load each image from the array in the correct order.

    Something like this...

    (This example assumes you have three movieclips on stage; mc1, mc2 and mc3, and also that you have three images next to your swf location called img1, img2 and img3. Simply change these names in the arrays to whatever you need to.)

    Actionscript Code:
    var mcArray:Array = new Array (mc1, mc2, mc3);
    var urlArray:Array = new Array ("img1.jpg","img2.jpg","img3.jpg");

    loadImages ();

    function loadImages ():void
    {
        //arrayIndex keeps track of the current array position
        var arrayIndex:int = 0;
        var loader:Loader = new Loader ();
       
        loader.contentLoaderInfo.addEventListener (Event.COMPLETE, imageLoaded);
       
        loop ();
       
        function loop ():void
        {
            //load an image from a url stored in the urlArray using the current array position (the arrayIndex variable)
            loader.load (new URLRequest (urlArray[arrayIndex]));
           
            /*
            the loop method gets called for as many urls there are in the urlArray, and is equivalent to doing the following:
           
            loader.load (new URLRequest (urlArray[0]));
            loader.load (new URLRequest (urlArray[1]));
            loader.load (new URLRequest (urlArray[2]));
            etc...
            */

           
        }

        function imageLoaded (event:Event):void
        {
            //create a holder movieclip (for each load complete operation) that can be repositioned within a main movieclip, effectively centring the registration point
            var holder:MovieClip = new MovieClip ();
           
            //add the loaded bitmap into the holder
            holder.addChild (event.currentTarget.loader.content);
           
            //position the holder using negative values. This moves it from top left of the main movieclip to half of its width and height.
            //the alpha is reduced to half so that you can see what is happening
            holder.x = -holder.width/2;
            holder.y = -holder.height/2;
            holder.alpha = .5;
           
            //add the holder to the display list of the main movieclip at the current array position, again, using the arrayIndex variable
            mcArray[arrayIndex].addChild (holder);
           
            //check if all the urls have been loaded, if not, increase the arrayIndex value by 1 and call the loop method again to load the next url in the urlArray
            if (arrayIndex < urlArray.length - 1)
            {
                arrayIndex++;
                loop ();
            }
            else //if all the urls have been loaded remove the listener for the complete event just to clean things up
            {
                    loader.contentLoaderInfo.removeEventListener (Event.COMPLETE, imageLoaded);
            }
        }
    }

    hope that helps

  4. #4
    Junior Member
    Join Date
    Feb 2009
    Posts
    17
    Good Stuff!

    All I had to do was take the array & loader out of the function and all worked well, even integrated a resizer very easily. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center