5 Replies Latest reply on Jan 31, 2007 11:45 AM by kglad

    I'm stuck after I delete onEnterFrame

      I've made a lot of progress on this file thanks to some help from the forum. I've reached a point I can't figure out. In my released() function, I need to call a delete onEnterFrame so that the scripted tweens can run. But how do I "reinstate" the onEnterFrame when the unreleased() function is called so that the original motion can run.

      import mx.utils.Delegate
      import mx.transitions.Tween;
      import mx.transitions.easing.*;

      var numClouds:Number = 35;
      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 home:MovieClip = this;

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

      var dreamarrayX:Array = new Array();
      dreamarrayX[0] = 0
      dreamarrayX[1] = -1800;
      dreamarrayX[2] = 2000;
      dreamarrayX[3] = 100;
      dreamarrayX[4] = -1700;

      var dreamarrayZ:Array = new Array();
      dreamarrayZ[0] = 0;
      dreamarrayZ[1] = 500;
      dreamarrayZ[2] = 1000;
      dreamarrayZ[3] = 1500;
      dreamarrayZ[4] = 2000;

      function init() {
      for (var i:Number = 0; i<dreamarray.length; i++) {
      var dream:MovieClip = home.attachMovie(dreamarray , "dreamarray" + i, i);
      dream.x = dreamarrayX
      ;
      dream.y = 50;
      dream.z = dreamarrayZ ;
      dream.onRelease = released;
      }
      }

      function inittwo() {
      //set Number to more than number of dreams
      for (var j:Number = 20; j<numClouds; j++) {
      var cloud:MovieClip = attachMovie("cloud", "cloud" + j, j);
      cloud.x = Math.random() * 2000 - 800;
      cloud.y = 50;
      cloud.z = 0 + cloudoffset;
      //cloud.onEnterFrame = mover;
      cloudoffset += 600;
      }
      }

      function released() {
      for (var i=0;i<dreamarray.length;i++){
      var t:MovieClip = home["dreamarray"+i];
      t.xPos = t._x;
      t.yPos = t._y;
      t.theScale = t._xscale;
      //delete dream.onRelease;
      delete onEnterFrame;
      if (t != this)
      {
      //trace(this);
      var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,0,1,true);
      var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,0,1,true);
      var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,100,0,1,true);
      //trace(t);
      //trace(t.xPos);
      //trace(t.yPos);
      }
      else
      {
      var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,55,1,true);
      var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,55,1,true);
      var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,200,1,true);
      var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,100,1,true);
      var tw5:Tween = new Tween(theText,"_alpha",Strong.easeOut,0,100,1,true);
      var s:Object = this;
      tw.onMotionStopped = function()
      {
      s.onRelease = unReleased;
      }
      }
      }
      }

      function unReleased(){
      //delete this.onRelease;
      for(var i=0;i<dreamarray.length;i++)
      {
      var t:MovieClip = home["dreamarray"+i];
      if(t != this)
      {
      var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,0,t.theScale,1,true);
      var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,0,t.theScale,1,true);
      var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,0,100,1,true);
      }
      else
      {
      var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,100,t.theScale,1,true);
      var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,100,t.theScale,1,true);
      var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,t.xPos,1,true);
      var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,t.yPos,1,true);
      tw.onMotionStopped = function()
      {
      for(var i=0;i<dreamarray.length;i++)
      {
      var t:MovieClip = home["dreamarray"+i];
      t.onRelease = Delegate.create(t,released);
      //t.onEnterFrame = mover;
      }
      }
      }
      }
      }





      function onEnterFrame():Void {
      //if (dreamarray4.z >= 500 && Key.isDown(Key.UP)){
      if (zdepth < 55 && Key.isDown(Key.UP)){
      vz -= 1;
      zdepth += 1;
      }

      //if(dreamarray4.z >=2000 && Key.isDown(Key.DOWN))
      if(zdepth > 0 && Key.isDown(Key.DOWN))
      {
      vz += 1;
      zdepth -= 1;
      }

      vy -= gravity;
      vx *= friction;
      vy *= friction;
      vz *= friction;
      for (var i:Number=0;i<dreamarray.length;i++) {
      var dream:MovieClip = home["dreamarray" +i];
      dream.x += vx;
      dream.y += vy;
      dream.z += vz;
      if(dream.y < 50)
      {
      dream.y = 50;
      vy = 0;
      }

      if (dream.z <= -fl) {
      //delete dream.z
      dream.z._visible = false;
      }
      else
      {
      dream.z._visible = true;

      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 += 5000;
      }
      else if(cloud.z > 5000 - fl)
      {
      cloud.z -= 5000;
      }

      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);
      }
      }


      /*//trying to create the mover function
      function mover():Void {
      //if (dreamarray4.z >= 500 && Key.isDown(Key.UP)){
      if (zdepth < 55 && Key.isDown(Key.UP)){
      vz -= 1;
      zdepth += 1;
      }

      //if(dreamarray4.z >=2000 && Key.isDown(Key.DOWN))
      if(zdepth > 0 && Key.isDown(Key.DOWN))
      {
      vz += 1;
      zdepth -= 1;
      }

      vy -= gravity;
      vx *= friction;
      vy *= friction;
      vz *= friction;
      for (var i:Number=0;i<dreamarray.length;i++) {
      var dream:MovieClip = home["dreamarray" +i];
      dream.x += vx;
      dream.y += vy;
      dream.z += vz;
      if(dream.y < 50)
      {
      dream.y = 50;
      vy = 0;
      }

      if (dream.z <= -fl) {
      //delete dream.z
      dream.z._visible = false;
      }
      else
      {
      dream.z._visible = true;

      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 += 5000;
      }
      else if(cloud.z > 5000 - fl)
      {
      cloud.z -= 5000;
      }

      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);
      }
      }
      */

      init();
      inittwo();


      stop();
        • 1. Re: I'm stuck after I delete onEnterFrame
          kglad Adobe Community Professional & MVP
          in general, if you want to stop a mc.onEnterFrame loop and you still want to be able to re-start it, you should use a slightly different set-up.

          define a function (say, function1) that contains the code you had in your mc.onEnterFrame loop, when you want that function code to loop use

          1 person found this helpful
          • 2. Re: I'm stuck after I delete onEnterFrame
            I changed my script to create dream.onEnterFrame = mover;
            The dream MovieClips attach correctly and move as they are supposed to....but now my clouds just get attached in the upperleft corner.

            I tried creating cloud.onEnterFrame = mover; since the clouds movement is controlled in the same function as the "dreams"

            Do I need to create a seperate mover function for the clouds? And rename all of my variables to go with the new function?


            import mx.utils.Delegate
            import mx.transitions.Tween;
            import mx.transitions.easing.*;

            var numClouds:Number = 35;
            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 home:MovieClip = this;

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

            var dreamarrayX:Array = new Array();
            dreamarrayX[0] = 0
            dreamarrayX[1] = -1800;
            dreamarrayX[2] = 2000;
            dreamarrayX[3] = 100;
            dreamarrayX[4] = -1700;

            var dreamarrayZ:Array = new Array();
            dreamarrayZ[0] = 0;
            dreamarrayZ[1] = 500;
            dreamarrayZ[2] = 1000;
            dreamarrayZ[3] = 1500;
            dreamarrayZ[4] = 2000;

            function init() {
            for (var i:Number = 0; i<dreamarray.length; i++) {
            var dream:MovieClip = home.attachMovie(dreamarray , "dreamarray" + i, i);
            dream.x = dreamarrayX
            ;
            dream.y = 50;
            dream.z = dreamarrayZ ;
            dream.onRelease = released;
            dream.onEnterFrame = mover;
            }
            }

            function inittwo() {
            //set Number to more than number of dreams
            for (var j:Number = 20; j<numClouds; j++) {
            var cloud:MovieClip = attachMovie("cloud", "cloud" + j, j);
            cloud.x = Math.random() * 2000 - 800;
            cloud.y = 50;
            cloud.z = 0 + cloudoffset;
            //cloud.onEnterFrame = mover;
            cloudoffset += 600;
            }
            }

            function released() {
            for (var i=0;i<dreamarray.length;i++){
            var t:MovieClip = home["dreamarray"+i];
            t.xPos = t._x;
            t.yPos = t._y;
            t.theScale = t._xscale;
            //delete dream.onRelease;
            t.onEnterFrame = null;
            if (t != this)
            {
            var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,0,1,true);
            var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,0,1,true);
            var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,100,0,1,true);

            }
            else
            {
            var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,t._xscale,55,1,true);
            var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,t._yscale,55,1,true);
            var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,200,1,true);
            var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,100,1,true);
            var tw5:Tween = new Tween(theText,"_alpha",Strong.easeOut,0,100,1,true);
            var s:Object = this;
            tw.onMotionStopped = function()
            {
            s.onRelease = unReleased;
            }
            }
            }
            }

            function unReleased(){
            for(var i=0;i<dreamarray.length;i++)
            {
            var t:MovieClip = home["dreamarray"+i];
            if(t != this)
            {
            var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,0,t.theScale,1,true);
            var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,0,t.theScale,1,true);
            var tw3:Tween = new Tween(t,"_alpha",Strong.easeOut,0,100,1,true);
            }
            else
            {
            var tw:Tween = new Tween(t,"_xscale",Strong.easeOut,100,t.theScale,1,true);
            var tw2:Tween = new Tween(t,"_yscale",Strong.easeOut,100,t.theScale,1,true);
            var tw3:Tween = new Tween(t,"_x",Strong.easeOut,t._x,t.xPos,1,true);
            var tw4:Tween = new Tween(t,"_y",Strong.easeOut,t._y,t.yPos,1,true);
            tw.onMotionStopped = function()
            {
            for(var i=0;i<dreamarray.length;i++)
            {
            var t:MovieClip = home["dreamarray"+i];
            t.onRelease = Delegate.create(t,released);
            t.onEnterFrame = mover;
            }
            }
            }
            }
            }

            //trying to create the mover function
            function mover():Void {
            if (zdepth < 55 && Key.isDown(Key.UP)){
            vz -= 1;
            zdepth += 1;
            }
            if(zdepth > 0 && Key.isDown(Key.DOWN))
            {
            vz += 1;
            zdepth -= 1;
            }

            vy -= gravity;
            vx *= friction;
            vy *= friction;
            vz *= friction;
            for (var i:Number=0;i<dreamarray.length;i++) {
            var dream:MovieClip = home["dreamarray" +i];
            dream.x += vx;
            dream.y += vy;
            dream.z += vz;
            if(dream.y < 50)
            {
            dream.y = 50;
            vy = 0;
            }

            if (dream.z <= -fl) {
            dream.z._visible = false;
            }
            else
            {
            dream.z._visible = true;

            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 += 5000;
            }
            else if(cloud.z > 5000 - fl)
            {
            cloud.z -= 5000;
            }

            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);
            }
            }

            init();
            inittwo();

            stop();
            • 3. Re: I'm stuck after I delete onEnterFrame
              kglad Adobe Community Professional & MVP
              if you want to use the same function (mover) for more than one movieclip, pass the movieclip to mover as a parameter. something like:

              1 person found this helpful
              • 4. Re: I'm stuck after I delete onEnterFrame
                thanks again for all the help. this forum rules! I understand replacing "dream" with mc and passing as a parameter, but i didn't follow what i should do with "cloud" where you say delete the duplicate cloud loop. I think i'm going to keep the clouds seperate anyway though, so i can control them differently then the "dreams".
                • 5. Re: I'm stuck after I delete onEnterFrame
                  kglad Adobe Community Professional & MVP
                  you're welcome.

                  cloud works the same as dream (or any other movieclip that you want to pass to mover). just call mover when you want to have it act on the movieclip that's passed to it.

                  and use the delete statement as shown to stop the onEnterFrame loops. if you want to stop an onEnterFrame loop based on some condition that you test within mover, use delete mc.onEnterFrame.