0 Replies Latest reply on Oct 30, 2007 2:36 PM by sixofeight

    Help with script - loops & arrays

    sixofeight
      Hello!

      I am trying to learn how to manage multiple moving objects, including the use of arrays, loops, dynamic content, etc. I have been through more than a few tutorials, but am still fuzzy on some of the syntax and structure in ActionScript. (I am using Flash 8 Pro, AS2)

      The following script (which works the way I want it to) imports a small (50x50) graphic from a file, and animates it to slide across a stage 900(w) x 150(h).
      - - - - - - -
      // Create a movie clip instance
      this.createEmptyMovieClip("img1_mc", 1);

      // Instructions to animate the graphic once loaded
      var mcl_obj:Object = new Object();
      mcl_obj.onLoadInit = function(target_mc:MovieClip):Void {
      target_mc._x = 1;
      target_mc._y = 50;
      target_mc.onEnterFrame = function() {
      target_mc._x += 20;

      // decrease current _x position by 3 pixels
      if (target_mc._x>=900) {
      target_mc._x = 900;
      delete target_mc.onEnterFrame;
      }
      };
      };

      var img_mcl:MovieClipLoader = new MovieClipLoader();
      img_mcl.addListener(mcl_obj);

      // Load an image into the movie clip
      img_mcl.loadClip("happy.png",img1_mc);
      - - - - - - -

      What I wanted to do next, was to use an array to animate a series of THREE graphics, all of which would slide across the stage in a little parade. I kludged together code using a combination of some cut & paste from other examples, my own testing, etc. Not surprisingly, I am having some trouble ... I suspect I have both some syntax mistakes and probably some structure errors ... maybe even a missing statement or three! But I am having trouble interpreting where I need help:

      Here is the ActionScript code I have so far:
      - - - - - - - -
      // names of graphic files, in order
      var picnames:Array = [
      "happy",
      "angel",
      "fruit"];

      // constants
      var PICPATH:String = "box/"; // folder with graphics
      var NPICS:Number = picnames.length; // total number of graphics

      // index to array
      var picnum:Number;

      // Create a movie clip instance for each graphic
      for (var k:Number = 0; k < NPICS; k++) {
      var mc:MovieClip = this.createEmptyMovieClip("stuff_mc"+k, k+1);
      }

      // move each graphic after it is finished loading
      var mcl_obj:Object = new Object();
      mcl_obj.onLoadInit = function(target_mc:MovieClip):Void {
      for (var t:Number = 0; t < NPICS; t++) {
      target_mc[t]._x = 1;
      target_mc[t]._y = 50;
      target_mc[t].onEnterFrame = function() {
      target_mc[t]._x += 20;
      // decrease current _x position by 3 pixels
      if (target_mc[t]._x>=900) {
      target_mc[t]._x = 900;
      delete target_mc[t].onEnterFrame;
      }
      }
      };
      };

      // set up an instance of MovieClipLoader named float
      var float:MovieClipLoader = new MovieClipLoader();
      float.addListener(mcl_obj);

      // set the pointer to the first graphic in the array picnames
      picnum = 0;
      // start loading graphics
      float.loadClip(PICPATH + picnames[picnum] + ".png", stuff_mc[picnum]);
      - - - - - - - - - -

      I cannot seem to find a good progressive tutorial which incorporates the steps I want to learn ... and many of the more advanced examples (such as for photo galleries) have so many additional bells & whistles it is too hard for me to see the things I need and separate them from the more complex tricks I don't.yet need.

      I would welcome any pointers regarding what mistakes I am making.

      Thanks!