4 Replies Latest reply on Nov 29, 2009 9:11 AM by NeronBurningRome

    loop for problem

    NeronBurningRome

      Hi,

       

      I want to show 4 thumbnails next to each other, I have this code:

       

      var imagesNumber:Number = 4;
      for (i=1; i<=imagesNumber; i++) {
          this.createEmptyMovieClip("thumb"+i, this.getNextHighestDepth());
          trace (thumb);
          thumb = "thumb"+i;
          thumb._x = i*200;
          thumb._y = 100;
          loadMovie("thumbs/thumb"+i+".jpg", thumb);
      }

       

      The result is that I see only the 4th thumb at the position of _x=0 and _y=0 instead of 4 thumbs.

      The 'trace' command returns this:

       

      "undefined
      thumb1
      thumb2
      thumb3"

       

      So why the first result of 'trace' command is 'undefined'? And why I see only 1 thumb with wrong x/y params?

       

      I know that it must be my mistake somewhere and I'll bet the solution is very simple, but I can't find it anywhere (for now, I'll keep looking). Thanks in advance for any help:)

        • 1. Re: loop for problem
          Ned Murphy Adobe Community Professional & MVP

          You're tracing thumb before it is a variable with a defined value.

           

          You should use the MovieClipLoader.loadClip methiod rather than loadMovie, and load the images one at a time.  The MovieClipLoader class supports having a listener so that you can tell when the image is loaded and trigger loading the next.  If you look up the MovieClipLoader.addListener method in the help documents, there is an example there that covers most of the elements of load a file.  You just want to have the completion of one loading trigger loading the next.

          • 2. Re: loop for problem
            NeronBurningRome Level 1

            I used MovieClipLoader and it works even without a listener. Would You recomend usuing listener anyway? If so should I use onLoadInit or onLoadComplete? And how to put listeners into a loop?

            • 3. Re: loop for problem
              Ned Murphy Adobe Community Professional & MVP

              I would use onLoadComplete since there isn't really an Init event that occurs.  To load things one after another, meaning wait until one finishes loading before starting the next, you need to use the event listener, otherwise you don't know when it's time to load another.  You have one function that initiates the loading process, which includes assigning the event listener, and another function that is called by the event listener when loading an image is complete.  That second function processes the loaded object, increments a counter for the next image to be loaded, and calls the loading function if that counter hasn't reached its end.  You essentially create a loop of your own without using coded loop functions (for, while).

               

              Don't take this example literally, but here's a general scenario that uses an array to list the images....

               

              var imageArray = array of images to be loaded.

               

              counter = 0;

               

              loadFunction(){

                   - define loader

                   - define listener for loader - calls loadCompleteFunction

                   - load imageArray[counter]

              }

               

              loadCompleteFunction(){

                   - process loaded image

                   - counter += 1

                   - call loadFunction if counter < imageArray.length

              }

              1 person found this helpful
              • 4. Re: loop for problem
                NeronBurningRome Level 1

                Thanks for help. I hope that an array is not required. I created something like this and it works fine:

                 

                var loader = new MovieClipLoader();
                var preloader = new Object();
                loader.addListener(preloader);

                 

                i=1;
                loadFunction()

                 

                function loadFunction(){
                    thumb_mc = this.createEmptyMovieClip(i, this.getNextHighestDepth());
                    loader.loadClip("thumbs/thumb"+i+".jpg", thumb_mc);
                    preloader.onLoadInit=function() {
                        loadCompleteFunction();
                    }
                }
                       
                function loadCompleteFunction(){

                    thumb_mc._x = i*100;
                     thumb_mc._y = 100;

                    i++;
                    if (i<5) {
                    loadFunction();
                }
                }

                 

                 

                Now if I'd need to load bigger image when the thumb is pressed it'll be enough to put onRelease event into the loadCompleteFunction?