Skip to main content
October 20, 2010
Question

Movement AS Script 2

  • October 20, 2010
  • 1 reply
  • 785 views

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..

Can any1 please suggests any tutorials good sites.

Many Thanks

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
October 20, 2010

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.

October 21, 2010

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

Ned Murphy
Legend
October 21, 2010

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.