# Help with contained x axis mouse avoid!

**URL:** <https://forum.kirupa.com/t/help-with-contained-x-axis-mouse-avoid/56684>\
**Category:** Uncategorized\
**Created:** [July 6, 2004, 7:48pm UTC](https://forum.kirupa.com/t/help-with-contained-x-axis-mouse-avoid/56684 "2004-07-06T19:48:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sodyPop](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@sodyPop](https://forum.kirupa.com/u/sodyPop)\
**Post date:** [July 6, 2004, 7:48pm UTC](https://forum.kirupa.com/t/help-with-contained-x-axis-mouse-avoid/56684/1 "2004-07-06T19:48:55Z")

</div>

i hope someone can help, i am stuck and i am a fairly mid level actionscripter so this is something i haven’t done before.

i have a little crab movie clip on the bottom of the ocean floor in a scene.  
i want the crab clip to avoid the mouse on the x axis only and i want it to be contained to the edges of the movie. i have found info on doing this on both and x and y axis and without being contained. i have also found info on this but without any gravity or friction to it which we need.

any help is much appreciated!

thanks,  
sodyPop

---

<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:** [July 11, 2004, 4:28am UTC](https://forum.kirupa.com/t/help-with-contained-x-axis-mouse-avoid/56684/2 "2004-07-11T04:28:27Z")

</div>

bump\> anyone?  
please help?

---

<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:** [July 11, 2004, 9:21pm UTC](https://forum.kirupa.com/t/help-with-contained-x-axis-mouse-avoid/56684/3 "2004-07-11T21:21:07Z")

</div>

basically, wouldn’t you just make the crab accelerate towards the side of the movie the mouse is farthest from?

Something like this:  
if you have a restriction on the crab’s left and right boundaries

var crabMin:Number = 0;  
var crabMax:Number = 100;

Then you check the current mouse position, and see whether it’s closer to the left or right.

var currentMouse:Number = \_xmouse;  
if((crabMax - currentMouse) \> currentMouse){ //Meaning the crab is more to the right  
// than the left (I think 🙂 )  
crabMC.\_x += (.98) \* crabMC.\_x;  
} else {  
crabMC.\_x -= (.98) \* crabMC.\_x;  
}

I think that may do it, but I haven’t actually tested that. Oh. and the .98 corresponds to friction. And the reason it’s greater that currentMouse is that crabMin is 0. Otherwise, it’d be a bit different.

If you’re willing to wait a while, I may get around to testing that. Otherwise, ask questions if you have them, and have fun. 🙂
