1 Reply Latest reply on Mar 20, 2011 4:01 PM by robdillon

    AS3 FL4 Move Movie Clip

    giohh

      suppose one has two buttons "left" and a "right ".
      the 'left' moves to the left
      and the "right" to the right
      and the goal is to move the object "ball"
      based on this potential requires that script to do this?

        • 1. Re: AS3 FL4 Move Movie Clip
          robdillon Most Valuable Participant

          Here's one version:

           

           

          var howFar:int = 5;


          left.addEventListener(MouseEvent.MOUSE_UP,moveLeft);

           

          right.addEventListener(MouseEvent.MOUSE_UP,moveRight);

           

          function moveLeft(event:MouseEvent):void {

            ball.x += howFar;

          }


          function moveRight(event:MouseEvent):void {

            ball.x -= howFar;

          }

           

          Just change the value of the variable howFar to change the distance that "ball" is moved. "Ball" will be moved every time one of the buttons is clicked.

           

          A second version:

           

          var howFar:int = 5;

          var moveLeft:Boolean = false;

          var moveRight:Boolean = false;

           

          left.addEventListener(MouseEvent.MOUSE_DOWN,leftDown);

          left.addEventListener(MouseEvent.MOUSE_UP,leftUp);

          right.addEventListener(MouseEvent.MOUSE_DOWN,rightDown);

          right.addEventListener(MouseEvent.MOUSE_UP,rightUp);

          stage.addEventListener(Event.ENTER_FRAME,onEf);

           

          function leftDown(event:MouseEvent):void {

               moveLeft = true;

          }


          function leftUp(event:MouseEvent):void {

               moveLeft = false;

          }


          function rightDown(event:MouseEvent):void {

               moveLeft = true;

          }


          function rightDown(event:MouseEvent):void {

               moveLeft = false;

          }

           

          function onEf(event:Event):void {

               if(moveRight) {

                    ball.x += howFar;

               }

               if(moveLeft) {

                    ball.x -= howFar;

               }

          }

           

          In this version the object "ball" will continue to move as long as the button left or right is held down. It will stop when the button is released.

           

          In either version, the "ball" will continue to move even after it has left the visible area of the stage.