4 Replies Latest reply on Jan 26, 2007 8:36 AM by

    Attach movie clips using seperate functions

      I'm having a little trouble with the following code. I'm calling two functions: init() and inittwo() to attach some movie clips to the stage. If I comment one of them out, the other works fine and the movement is okay. But as it is, only the "cloud" movie clips are being attached. I need for the clips in the "dreamarray" to be attached, and then for the "clouds" to be attached with a second function. I'm probably not going about this the right way, but I could sure use some help!

      It would also be cool if I could attach the dreams at set "x" values instead of random distribution. I'm not sure how to set a seperate x value for each item in the array being attached.

      var numClouds:Number = 10;
      var fl:Number = 250;
      var gravity:Number = .5;
      var vx:Number = 0;
      var vy:Number = 0;
      var vz:Number = 0;
      var friction:Number = .97;
      var vpX:Number = Stage.width / 2;
      var vpY:Number = Stage.height / 2;
      var vpY:Number = 400;

      var dreamarray:Array = new Array();
      dreamarray[0] = "dreamone";
      dreamarray[1] = "dreamtwo";
      dreamarray[2] = "dreamthree";

      init();
      inittwo();
      function init() {
      for (var i:Number = 0; i<dreamarray.length; i++) {
      var dream:MovieClip = attachMovie(dreamarray , "dreamarray" + i, i);
      //var dream:MovieClip = attachMovie("dreamone", "dreamone" + i, i);
      dream.x = Math.random() * 1600 - 800;
      dream.y = 50;
      dream.z = Math.random() * 800;
      //trace("functioncall");
      //tree.x = Math.random() * 2000 - 1000;
      //tree.y = 50;
      //tree.z = Math.random() * 10000;
      }
      }

      function inittwo() {
      for (var j:Number = 0; j<numClouds; j++) {
      var cloud:MovieClip = attachMovie("cloud", "cloud" + j, j);
      //tree.x = 400;
      cloud.x = Math.random() * 1600 - 800;
      cloud.y = 50;
      cloud.z = Math.random() * 800;
      }
      }

      function onEnterFrame():Void {
      if(Key.isDown(Key.UP))
      {
      vz -= 1;
      }
      else if(Key.isDown(Key.DOWN))
      {
      vz += 1;
      }
      if(Key.isDown(Key.LEFT))
      {
      vx += 1;
      }
      else if(Key.isDown(Key.RIGHT))
      {
      vx -= 1;
      }
      if(Key.isDown(Key.SPACE))
      {
      vy += 1;
      }
      vy -= gravity;
      vx *= friction;
      vy *= friction;
      vz *= friction;
      for (var i:Number=0;i<dreamarray.length;i++) {
      //i think there is a error here
      var dream:MovieClip = this["dreamarray" +i];
      dream.x += vx;
      dream.y += vy;
      dream.z += vz;
      if(dream.y < 200)
      {
      dream.y = 200;
      vy = 0;
      }
      if (dream.z <= -fl) {
      dream.z += 1000;
      }
      else if(dream.z > 1000 - fl)
      {
      dream.z -= 1000;
      }
      var scale:Number = fl / (fl + dream.z);
      dream._xscale = dream._yscale = scale*100;
      dream._x = vpX + dream.x * scale/2;
      dream._y = vpY + dream.y * scale/2;
      //dream._x = dream.x * scale/2;
      dream._alpha = scale * 60 + 40;
      dream.swapDepths(-dream.z);
      }

      for (var j:Number=0;j<numClouds;j++) {
      var cloud:MovieClip = this["cloud" + j];
      cloud.x += vx;
      cloud.y += vy;
      cloud.z += vz;
      if(cloud.y < 50)
      {
      cloud.y = 50;
      vy = 0;
      }
      if (cloud.z <= -fl) {
      cloud.z += 500;
      }
      else if(cloud.z > 10000 - fl)
      {
      cloud.z -= 10000;
      }
      var scale:Number = fl / (fl + cloud.z);
      cloud._xscale = cloud._yscale=scale*200;
      cloud._x = vpX + cloud.x * scale;
      cloud._y = vpY + cloud.y * scale;
      cloud._alpha = scale * 60 + 40;
      cloud.swapDepths(-cloud.z);
      }
      }
        • 1. Attach movie clips using seperate functions
          DazFaz Level 1
          Hi Ive just taken a very quick look at your code and Ive noticed at least one or two mistakes.

          you are calling:
          init();
          inittwo();

          before the actual functions have been declared.

          Flash works from top to bottom when actioning code so by calling:
          init();
          inittwo();
          bofore the function has been declared and this will do nothing as it cannot find the functions to call as they have been declared AFTER the function calls.

          The best thing to do is put any calls right at the bottom and then you will know everything is ready to be triggered.
          1 person found this helpful
          • 2. Re: Attach movie clips using seperate functions
            That makes sense. I moved the function calls to the bottom, but the movie still functions exactly as it did before. So far, the entire movie is just one frame, so I guess the functions were being called as it continues to loop in frame 1. Something is still incorrect in my code. I'm thinking it is a "}" in the wrong place (maybe) but I can't seem to figure it out. Thanks for the tip though! I'll keep that in mind as a move forward.
            • 3. Re: Attach movie clips using seperate functions
              Okay, I answered my own question. I was using two functions to attach movie clips to the stage using a loop, but both functions were trying to attach the movie clips to the same depth...so the first movie clips were being replaced by the second function call.
              • 4. Re: Attach movie clips using seperate functions
                Okay, I answered my own question. I was using two functions to attach movie clips to the stage using a loop, but both functions were trying to attach the movie clips to the same depth...so the first movie clips were being replaced by the second function call.