# \[as1\] mouse repel/avoidance

**URL:** <https://forum.kirupa.com/t/as1-mouse-repel-avoidance/103297>\
**Category:** Uncategorized\
**Created:** [March 3, 2004, 9:39pm UTC](https://forum.kirupa.com/t/as1-mouse-repel-avoidance/103297 "2004-03-03T21:39:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![massive3215](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/massive3215/32/253_2.png) [@massive3215](https://forum.kirupa.com/u/massive3215)\
**Post date:** [March 3, 2004, 9:39pm UTC](https://forum.kirupa.com/t/as1-mouse-repel-avoidance/103297/1 "2004-03-03T21:39:21Z")

</div>

I’m trying to make a movieclip avoid my mouse, like on this site:[http://exopolis.com](http://exopolis.com). I want the movieclip to never leave a defined area (200 x 200). I’m having a little trouble getting it to work. Here’s my code on the movieclip:

onClipEvent (enterFrame) {  
if (\_root.\_xmouse\<200 && \_root.\_xmouse\>0) {  
//x movement  
mx = \_root.\_xmouse;  
if (mx\<\_x) {  
dx = \_x-mx;  
} else {  
dx = mx-\_x;  
}  
moveSpeedx = dx/30;  
if (mx\>\_x) {  
\_x = \_x-moveSpeedx;  
} else {  
\_x = \_x+moveSpeedx;  
}  
}  
if (\_root.\_ymouse\<200 && \_root.\_ymouse\>0) {  
//y movement  
my = \_root.\_ymouse;  
if (my\>\_y) {  
dy = \_y+my;  
} else {  
dy = my+\_y;  
}  
moveSpeedy = dy/30;  
if (my\<\_y) {  
\_y = \_y+moveSpeedy;  
} else {  
\_y = \_y-moveSpeedy;  
}  
}  
}

Right now the mouse is still leaving the defined area, and it slows down when my mouse gets closer rather. Any help would be appreciated.
