4 Replies Latest reply on Mar 17, 2008 11:13 AM by surfmad1

    How to stop a movie clip moving

    surfmad1
      I'm not newbie to flash but I am newbie to coding using actionscript!!!

      I am trying to create a button that moves a movie clip ("content1") diagonally across the screen but once the movie clip reaches a certain x co-ordinate I want it to stop and then when I click another button I would like it to return to it's starting position. At the moment the movie clip moves if I click one of the buttons but as soon as I click another one it stops which it shouldn't it should go back in the other direction. The other problem is that once I've clicked a button I cant seem to get it to stop moving once it reaches a certain _x position it just keeps on moving off the stage.

      Below is the script I have so far not including any conditions to check if "content1" has reached it _x position

      Could anyone help please!!!!

      -------------------------------------------
      //on Right movie clip containing the button
      onClipEvent (enterFrame) {
      _root.content1._x += speedX;
      _root.content1._y -= speedY;
      }
      -------------------------------------------
      //on Right button inside movie clip
      on (press) {
      speedX = 5.5;
      speedY = 1.33;
      }
      -------------------------------------------
      //on Left movie clip containing the button
      onClipEvent (enterFrame) {
      _root.content1._x += speedX;
      _root.content1._y -= speedY;
      }
      -------------------------------------------
      //on Left button inside movie clip
      on (press) {
      speedX = 5.5;
      speedY = 1.33;
      }
      -------------------------------------------
        • 1. How to stop a movie clip moving
          clbeech Level 3
          hi there - a much more efficient and effective way to do this is to use the Tween class. also you should always try to place your codes on the main timeline as a best practice, the code below should go in the first frame of the main timeline, the 'mc' should be the instance name of the MC you want to move, and the 'right_btn' and 'left_btn' should be the instance names of your buttons.

          it would look something like the following depending on the position that you want it to go to.

          • 2. Re: How to stop a movie clip moving
            surfmad1 Level 1
            Many thanks ClBeech!!!

            I've tried the code you suggested but it doesn't seem to work :0(

            I'm working on a Mac using Flash Pro 8 which only has the option to publish as 'actionscript 1.0 or 2.0' is the script you supplied an actionscript 3.0?

            Below is the script with the amendments, I've changed my button instance names to match the script you supplied (left_btn / right_btn)

            stop();
            import mx.transitions.Tween;
            import mx.transitions.easing.Strong;
            var rposX = 369;
            //the ending x of the right position
            var rposY = 84;
            //the ending y of the right position
            var lposX = 116;
            //the ending x of the left position
            var lposY = 154;
            //the ending y of the left position
            right_btn.onPress = function() {
            new Tween(content1, '_x', Strong.easeOut, content1._x, rposX, time, false);
            new Tween(content1, '_y', Strong.easeOut, content1._y, rposY, time, false);
            };
            left_btn.onPress = function() {
            new Tween(content1, '_x', Strong.easeOut, content1._x, lposX, time, false);
            new Tween(content1, '_y', Strong.easeOut, content1._y, lposY, time, false);
            };

            • 3. Re: How to stop a movie clip moving
              clbeech Level 3
              LMAO!!! hey surfmad1 - i see what the prob is here, looks like you have everything correct, provided that you do have the instance names on the clips as you've labeled. BUT - the reall problem is that i forgot to add one very important variable to the list ... 'time' has not been declared lol!! so set it as below with the other vars, and use a number related to the quantity of 'frames' worth of time that you want to make the move in:

              var time = 30; //30frames worth (1sec at 30 fps)

              ps. yeah no problem with the code, it's AS2, I run mac myself and Flash 8 as well :)
              • 4. Re: How to stop a movie clip moving
                surfmad1 Level 1
                Many thanks clBeech!!! Is there an easy way to make one button do the same thing eg: 1st click the mc moves to the right then on the 2nd click it moves to the left. I guess I need to add in an 'if & else' statement to check the position of the mc but I'm not too sure how to implement it.

                thanks again for all your help :)