[FMX]Moving mc from xpos to xpos and back

I have a mc on the stage. I want the mc to move from one x position to another x position and back to it’s previous x position again, after a button is pressed.

I have the following function:

function moveClip (clip, xPos, speed){
clip.onEnterFrame = function (){
eindX = xPos - this._x;
this._x += eindX / speed;
if (Math.abs(eindX) < 2){
this._x = xPos;
delete this.onEnterFrame;
}
 
};
}

Is this the right function to accomplish what I want and if yes, what should be the action for the button?

Thanks in advance,
Donald

myDestinations = [200, 500, 400] ;
i = 0 ;
yourButton.onPress = function () {
    moveClip (yourClip, myDestinations *, 5) ;
    i = (i+1)%myDestinations.length ;
} ;

Something like this.

Hi Ilyas,

Thanks for your reply. It is almost what I was looking for. But now I have to press tree times to go to the different x positions but what I need Is that with just one button press the mc is first going from lets say _x 400 to _x 300 and back to _x 400 again.

I hope this is not to difficult. :slight_smile:

Thank in advance

Like this?

function moveInOut (clip, xPos, speed){
    var xStart = clip._x ;
    var xTarget = xPos ;
    clip.onEnterFrame = function (){
        var dX = xTarget - this._x;
        this._x += dX / speed;
        if (Math.abs(dX) < 2){
            if (xTarget == xPos) {
                xTarget = xStart ;
            }
            else {
                this._x = xStart;
                delete this.onEnterFrame;
            }
        } 
    };
} ;

yourButton.onPress = function () {
    moveInOut (youClip, 400, 5) ;
} ;

Hi Ilyas,

That is where I was looking for. You’re knowledge is realy irreplaceable.

Thanks a lot :slight_smile: