11 Replies Latest reply on Oct 21, 2010 9:11 AM by Ned Murphy

    Movement AS Script 2

    Jim_Bob123

      This below helps illustrate what i am aiming to create, for example you click on button1 and then button1 moves from 1 spot in a horizontal direction to another, and click on button1 again it will move backwards, to its orignal pos. Needing advice on this..

       

      Guide.png

      Can any1 please suggests any tutorials good sites.

       

      Many Thanks

        • 1. Re: Movement AS Script 2
          Ned Murphy Adobe Community Professional & MVP

          Here's a somewhat simple example with a little commentary you can try.  I recommend opening a new file just to test and become familiar with it.  Add a button to the stage and give it an instance name "btn1".  Then place this code in the timeline...

           

          import mx.transitions.Tween;

           

          var btn1MoveOut = true;   // indicate initial direction to move
          var btn1StartX = btn1._x; // store starting position

           

          btn1.onRelease = function(){

           
              // assign the position the btn will moveTo

              // if btn1 is going to move out, assign that position (btn1StartX + 100)
              // otherwise assign startX as the destination
               var moveTo = btn1MoveOut ? btn1StartX+100 : btn1StartX;

           

              // move btn1 from wherever it is to the moveTo value in 0.5 seconds

              var moveBtn:Tween = new Tween(btn1, "_x", null, btn1._x, moveTo, 0.5, true);
             
              btn1MoveOut = !btn1MoveOut;  // switch direction for next click
          }

           

          Note: I just add 100 pixels to the intial position for the final position. Use whatever you intend in place of that.

          1 person found this helpful
          • 2. Re: Movement AS Script 2
            Jim_Bob123 Level 1

            I am trying to get the a few buttons to move in diagonal position

             

            this is the code so far that does move diagonally, but not straight on the way back it jumps. What am I doing wrong?

             

            //btn5 goes up Bottom Right///////////////////////////////////////////
            var btn5MoveOut = true;   // indicate initial direction to move
            var btn5StartY = btn5._Y; // store starting position
              var btn5StartX = btn5._X;

            btn5.onRelease = function(){    // if this is going to move in, assign startX as the destination
                // otherwise assign the move out position
            (btn5StartY - 50)

            var moveTo = !btn5MoveOut ? btn5StartY : btn5StartY - 25;

            (btn5StartX + 75)
            var moveTo = !btn5MoveOut ? btn5StartX : btn5StartX + 100;

                // move btn1 from wherever it is to the moveTo value in 0.5 seconds
                var moveBtn:Tween = new Tween(btn5, "_X", null, btn5._X, moveTo, 0.5, true);
               
            var moveBtn:Tween = new Tween(btn5, "_Y", null, btn5._Y, moveTo, 0.5, true);

                btn5MoveOut = !btn5MoveOut;  // switch direction for next click
            }

             

            Thanks for the help

            • 3. Re: Movement AS Script 2
              Ned Murphy Adobe Community Professional & MVP

              I didn't do more than a quick look so I don't know if I missed something else, but you need to have a moveToX and a moveToY value if you are moving relative to both.

              • 4. Re: Movement AS Script 2
                Jim_Bob123 Level 1

                Do you mean placing the movetoX & movetoY to highlighted places, again it works but isnt fluent when moving? How would you move the button in diagonal movement?

                //btn5 goes up Bottom Right///////////////////////////////////////////
                var btn5MoveOut = true;   // indicate initial direction to move
                var btn5StartY = btn5._Y; // store starting position
                  var btn5StartX = btn5._X;

                btn5.onRelease = function(){    // if this is going to move in, assign startX as the destination
                    // otherwise assign the move out position
                (btn5StartY + 100)
                (btn5StartX + 100)

                var moveToX = !btn5MoveOut ? btn5StartY : btn5StartY + 100;
                var moveToY = !btn5MoveOut ? btn5StartX : btn5StartX + 100;

                    // move btn1 from wherever it is to the moveTo value in 0.5 seconds
                    var moveBtn:Tween = new Tween(btn5, "_X", null, btn5._X, moveToX, 0.5, true);
                   
                var moveBtn:Tween = new Tween(btn5, "_Y", null, btn5._Y, moveToY, 0.5, true);

                    btn5MoveOut = !btn5MoveOut;  // switch direction for next click
                }

                 

                Thanks

                • 5. Re: Movement AS Script 2
                  Ned Murphy Adobe Community Professional & MVP

                  Use _x not _X, same for y

                   

                  Also, what is wrong with your code here? look closely...

                   

                  var moveToX = !btn5MoveOut ? btn5StartY : btn5StartY + 100;
                  var moveToY = !btn5MoveOut ? btn5StartX : btn5StartX + 100;

                  • 6. Re: Movement AS Script 2
                    Jim_Bob123 Level 1

                    I've manipulated your code, to make the circular buttons move up, down, left, right. But I can't seem to understand how to move diagonally and then returning back to the normal position?

                    + I can't see why that code is incorrect?

                    • 7. Re: Movement AS Script 2
                      Ned Murphy Adobe Community Professional & MVP

                      Try reading out loud to yourself, just one of the two lines.  Hopefully it will eventually catch.  There are some errors that it's better you find them yourself for the sake of trying to be more careful in the future so you can avoid asking other people to catch them for you.

                      • 8. Re: Movement AS Script 2
                        Jim_Bob123 Level 1

                        Good man Ned!! after long persistence i've spotted the problem, and it seems to be moving in the correct position. Appreciate the support thanks

                         

                        //btn5 goes up Bottom Right///////////////////////////////////////////
                        var btn5MoveOut = true;   // indicate initial direction to move
                        var btn5StartY = btn5._Y; // store starting position
                          var btn5StartX = btn5._X;

                        btn5.onRelease = function(){    // if this is going to move in, assign startX as the destination
                            // otherwise assign the move out position
                        (btn5StartY + 100)
                        (btn5StartX + 100)

                        var moveToY = !btn5MoveOut ? btn5StartY : btn5StartY + 100;
                        var moveToX = !btn5MoveOut ? btn5StartX : btn5StartX + 100;
                         
                            // move btn1 from wherever it is to the moveTo value in 0.5 seconds
                            var moveBtn:Tween = new Tween(btn5, "_x", null, btn5._x, moveToX, 0.5, true);
                           
                        var moveBtn:Tween = new Tween(btn5, "_y", null, btn5._y, moveToY, 0.5, true);

                            btn5MoveOut = !btn5MoveOut;  // switch direction for next click
                        }

                        • 9. Re: Movement AS Script 2
                          Ned Murphy Adobe Community Professional & MVP

                          You're welcome

                          • 10. Re: Movement AS Script 2
                            Jim_Bob123 Level 1

                            Looking at the same idea, Can you advise me on the following.. how can I get all the buttons with one click to start/stop moving in circular motion? All I have is buttons on stage??

                            • 11. Re: Movement AS Script 2
                              Ned Murphy Adobe Community Professional & MVP

                              Use Google and search "AS2 circular path" or similar terms.  It is a more complicated design to make, so you will do best to find a tutorial to learn from.