3 Replies Latest reply on May 30, 2012 10:47 AM by esdebon

    Making this a smooth shuffle

    JBonavita

      I'm working on the shell game - the game where you have to find which shell the ball is under.

       

      I'm trying to figure out how to shuffle these shells move than once and how to make the shuffle smooth. I would like the shuffle function to get called 6 times but each one has to be called once the current one is complete. I was thinking I might have to use MOTION_FINISH for each of the tweens in the finish1 and finish2 but I feel that there should be an easier way to do this.

       

       

      Here's the code I have so far:


      import fl.transitions.TweenEvent;
      import flash.events.MouseEvent;
      import flash.display.MovieClip;
      import fl.transitions.Tween;

      var shellTmp1:MovieClip;
      var shellTmp2:MovieClip;
      var finishX1;
      var finishX2;
      var timeTweens:int = 5;
      var newScaleX:Number=0.8;
      var newScaleY:Number=0.8;
      var newScaleX2:Number = 1.2;
      var newScaleY2:Number = 1.2;


      btnTest.addEventListener(MouseEvent.CLICK, initShuffle);

      function initShuffle(event:MouseEvent):void{
      shuffle();
      }

      function shuffle():void{
      shellTmp1 = shell;
      shellTmp2 = shell2;
      setChildIndex(shellTmp2, 5);
      var x1 = shellTmp1.x;
      var x2 = shellTmp2.x;
      finishX1 = x2;
      finishX2 = x1;
      shuffleShell(shellTmp1, finishX1, true);
      shuffleShell(shellTmp2, finishX2, false);

      }

      function shuffleShell(shellObj:MovieClip, finishX:Number, use1:Boolean){

      if (use1){
        var xTween:Tween = new Tween(shellObj, "scaleX", null, 1, newScaleX, timeTweens, false);
        tweens.push(xTween);
        tweens.push(new Tween(shellObj, "scaleY", null, 1, newScaleY, timeTweens, false));
        tweens.push(new Tween(shellObj, "x", null, shellObj.x, finishX * .90, timeTweens,false));
        xTween.addEventListener(TweenEvent.MOTION_FINISH, finish1);
      }
       
      else{
        var xTween2:Tween = new Tween(shellObj, "scaleX", null, 1, newScaleX2, timeTweens, false);
        tweens.push(xTween2); 
        tweens.push(new Tween(shellObj, "scaleY", null, 1, newScaleY2, timeTweens, false));
        tweens.push(new Tween(shellObj, "x", null, shellObj.x, finishX * .90, timeTweens,false));
        xTween2.addEventListener(TweenEvent.MOTION_FINISH, finish2);
      }
       
      }

      function finish1(e:TweenEvent):void{
          tweens.push(new Tween(shellTmp1, "x", null, finishX1 * .90, finishX1, timeTweens, false));
          tweens.push(new Tween(shellTmp1, "scaleX", null, newScaleX, 1, 2,false));
          tweens.push(new Tween(shellTmp1, "scaleY", null, newScaleY, 1, 2,false));
      }

      function finish2(e:TweenEvent):void{
          tweens.push(new Tween(shellTmp2, "x", null, finishX2 * .90, finishX2, timeTweens, false));
          tweens.push(new Tween(shellTmp2, "scaleX", null, newScaleX2, 1, 2,false));
          tweens.push(new Tween(shellTmp2, "scaleY", null, newScaleY2, 1, 2,false));
      }

       


      Thanks for any help

      Joe

        • 1. Re: Making this a smooth shuffle
          esdebon Level 4

          import fl.transitions.TweenEvent;

          import flash.events.MouseEvent;

          import flash.display.MovieClip;

          import fl.transitions.Tween;

           

          var shellTmp1:MovieClip;

          var shellTmp2:MovieClip;

          var finishX1;

          var finishX2;

          var timeTweens:int=5;

          var newScaleX:Number=0.8;

          var newScaleY:Number=0.8;

          var newScaleX2:Number=1.2;

          var newScaleY2:Number=1.2;

           

           

          btnTest.addEventListener(MouseEvent.CLICK, initShuffle);

           

          function initShuffle(event:MouseEvent):void {

             shuffle(6);

          }

          var times:int;

          function shuffle(times:int):void {

              this.times=times;

              shellTmp1=shell;

              shellTmp2=shell2;

              setChildIndex(shellTmp2, numChildren-1);

              var x1=shellTmp1.x;

              var x2=shellTmp2.x;

              finishX1=x2;

              finishX2=x1;

              shuffleShell(shellTmp1, finishX1, true);

              shuffleShell(shellTmp2, finishX2, false);

          }

           

          function shuffleShell(shellObj:MovieClip, finishX:Number, use1:Boolean) {

           

              if (use1) {

                  var xTween:Tween=new Tween(shellObj,"scaleX",null,1,newScaleX,timeTweens,false);

                  tweens.push(xTween);

                  tweens.push(new Tween(shellObj, "scaleY", null, 1, newScaleY, timeTweens, false));

                  tweens.push(new Tween(shellObj, "x", null, shellObj.x, finishX * .90, timeTweens,false));

                  xTween.addEventListener(TweenEvent.MOTION_FINISH, finish1);

              } else {

                  var xTween2:Tween=new Tween(shellObj,"scaleX",null,1,newScaleX2,timeTweens,false);

                  tweens.push(xTween2);

                  tweens.push(new Tween(shellObj, "scaleY", null, 1, newScaleY2, timeTweens, false));

                  tweens.push(new Tween(shellObj, "x", null, shellObj.x, finishX * .90, timeTweens,false));

                  xTween2.addEventListener(TweenEvent.MOTION_FINISH, finish2);

              }

           

          }

           

          function finish1(e:TweenEvent):void {

              tweens.push(new Tween(shellTmp1, "x", null, finishX1 * .90, finishX1, timeTweens, false));

              tweens.push(new Tween(shellTmp1, "scaleX", null, newScaleX, 1, 2,false));

              tweens.push(new Tween(shellTmp1, "scaleY", null, newScaleY, 1, 2,false));

              if(--times>0)

              shuffle(this.times)

          }

           

          function finish2(e:TweenEvent):void {

              tweens.push(new Tween(shellTmp2, "x", null, finishX2 * .90, finishX2, timeTweens, false));

              tweens.push(new Tween(shellTmp2, "scaleX", null, newScaleX2, 1, 2,false));

              tweens.push(new Tween(shellTmp2, "scaleY", null, newScaleY2, 1, 2,false));

          • 2. Re: Making this a smooth shuffle
            JBonavita Level 1

            Thanks for your response esdebon. This doesn't really work since the tweens may not have completed before shuffle is called again.

            • 3. Re: Making this a smooth shuffle
              esdebon Level 4

              I see there is an array that is mentioned but not seen in the code.

                   if (- times> 0)
                   shuffle (this.times)

              should be called when tween ends

              I put it where:
              xTween2.addEventListener (TweenEvent.MOTION_FINISH, finish2);

              but if not this place is because maybe there are other tweens