3 Replies Latest reply on Sep 9, 2009 3:22 PM by Ned Murphy

    position an image after it loads

    abna Level 1

      I've placed an empty movieclip on the stage and am using the following AS2 to randomly load an image into it from an xml file. What I can't figure out, though (and yes, I've spent hours googling it) is how to position the image on the stage at the top-right. I know it should be picture_mc.x = Stage.width - picture_mc._width; or something like that, but where do I put this code so that it gets the width *after* the image is loaded?

       

      xmlImages = new XML();
      xmlImages.ignoreWhite = true;
      xmlImages.onLoad = loadImages;
      xmlImages.load("../flash/people.xml");

       

      function loadImages(loaded) {
          if (loaded) {
              xmlFirstChild = this.firstChild;
              imageFileName = [];
              totalImages = xmlFirstChild.childNodes[0].childNodes.length;
              for (i=0; i<totalImages; i++) {
                  imageFileName[i] = xmlFirstChild.childNodes[0].childNodes[i].attributes.title;
              }
              randomImage();
          }
      }
      function randomImage() {
          if (loaded == filesize) {
              var ran = Math.round(Math.random() * (totalImages - 1));
              picture_mc.loadMovie(imageFileName[ran], 1);
          }
      }

        • 1. Re: position an image after it loads
          Ned Murphy Adobe Community Professional & MVP

          If you do not know the dimensions of the image ahead of time, then you need to use MovieClipLoader.loadClip() instead of loadMovie.  The reason being, loadMovie does not support having an event listener for detecting when loading is complete, but MovieClipLoader.loadClip() does.  If you look up the MovieClipLoader class in the Flash help files, and then look at the addListener() method of that class, there is an exmaple that will help you get it coded for your situation.... you may want to use the onLoadComplete event rather than the one the examples uses (onLoadInit)

          • 2. Re: position an image after it loads
            abna Level 1

            I've tried that, and it didn't work. From trace, the code below seems to correctly set the x & y of the movieclip, and it does show the image on the stage. But, even though the x & y values are correct, the image is not in the correct spot. It is positioned at 0,0. Using onLoadComplete, it doesn't find the width of the image correctly (it shows as 0), but onLoadInit does, so that's where I put the x, y stuff.

             

             

             

             

            xmlImages = new XML();
            xmlImages.ignoreWhite = true;
            xmlImages.onLoad = loadImages;
            xmlImages.load("../flash/people.xml");

             

            var loadListener:Object = new Object();

             

            loadListener.onLoadComplete = function(target_mc:MovieClip, httpStatus:Number):Void {
                trace(">> loadListener.onLoadComplete()");
                trace(target_mc._width); // 0
            }

             

            loadListener.onLoadInit = function(target_mc:MovieClip):Void {
                trace(">> loadListener.onLoadInit()");
               
                target_mc.x = Stage.width - target_mc._width;
                target_mc.y = 0;
               
            trace(Stage.width);    //590
            trace(target_mc._width);//208
            trace(target_mc.x);    //382
            trace(target_mc.y);    //0
            trace(mc._width);//208
            trace(mc.x);    //382
            trace(mc.y);    //0
            }

             

            var mcLoader:MovieClipLoader = new MovieClipLoader();
            mcLoader.addListener(loadListener);

             

            var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());

             

            function loadImages(loaded) {
                if (loaded) {
                    xmlFirstChild = this.firstChild;
                    imageFileName = [];
                    totalImages = xmlFirstChild.childNodes[0].childNodes.length;
                    for (i=0; i<totalImages; i++) {
                        imageFileName[i] = xmlFirstChild.childNodes[0].childNodes[i].attributes.img;
                    }
                    randomImage();
                }
            }
            function randomImage() {
                if (loaded == filesize) {
                    var ran = Math.round(Math.random() * (totalImages - 1));

                    mcLoader.loadClip(imageFileName[ran], mc);

                }
            }

            • 3. Re: position an image after it loads
              Ned Murphy Adobe Community Professional & MVP

              You want to use AS2 positioning code (_x/_y), not AS3 (x/y).  See if fixing that fixes it all.

              1 person found this helpful