# \[FMX\]Moving mc from xpos to xpos and back

**URL:** https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649
**Category:** Uncategorized
**Created:** [May 29, 2004, 8:19am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649 "2004-05-29T08:19:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dboers](https://avatars.discourse-cdn.com/v4/letter/d/bbe5ce/32.png) [@dboers](https://forum.kirupa.com/u/dboers)
#### Post date: [May 29, 2004, 8:19am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649/1 "2004-05-29T08:19:39Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 29, 2004, 9:12am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649/2 "2004-05-29T09:12:52Z")

</div>

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

```

Something like this.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 29, 2004, 9:25am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649/3 "2004-05-29T09:25:27Z")

</div>

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

Thank in advance

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 29, 2004, 9:32am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649/4 "2004-05-29T09:32:29Z")

</div>

Like this?

```auto
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) ;
} ;

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [May 29, 2004, 9:38am UTC](https://forum.kirupa.com/t/fmx-moving-mc-from-xpos-to-xpos-and-back/111649/5 "2004-05-29T09:38:15Z")

</div>

Hi Ilyas,

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

Thanks a lot 🙂
