# Direction detection

**URL:** https://forum.kirupa.com/t/direction-detection/186203
**Category:** Uncategorized
**Created:** [April 19, 2006, 6:24pm UTC](https://forum.kirupa.com/t/direction-detection/186203 "2006-04-19T18:24:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Exos\_Sho](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Exos\_Sho](https://forum.kirupa.com/u/Exos_Sho)
#### Post date: [April 19, 2006, 6:24pm UTC](https://forum.kirupa.com/t/direction-detection/186203/1 "2006-04-19T18:24:58Z")

</div>

based on [kirupa.com - Detecting Direction of Mouse Movement](http://www.kirupa.com/developer/actionscript/detecting_mouse_direction.htm)  
what it does is, it draws either 10 pixls right, when u move your mouse right, it moves 10 pixls left when u move your mouse left etc, and it draws 10 pxls up, left down, right depending on how u move ur mouse

> Mouse.hide();  
> var posX:Number = 275;  
> var posY:Number = 400;  
> var chang:Number = 10;  
> this.createEmptyMovieClip(“branch\_mc”, -1000);  
> branch\_mc.lineStyle(0.25, 0xff0000, 100);  
> branch\_mc.moveTo(posX, posY);  
> checkX = function (dx, oldVal, newVal) {  
> if (oldVal\<newVal && posX\<550) {  
> trace(“moving right”);  
> posX = posX+chang;  
> branch\_mc.lineTo(posX, posY);  
> } else if (oldVal\>newVal && posX\>0) {  
> trace(“moving left”);  
> posX = posX-chang;  
> branch\_mc.lineTo(posX, posY);  
> }  
> return newVal;  
> };  
> checkY = function (dy, oldVal, newVal) {  
> if (oldVal\<newVal && posY\<400) {  
> trace(“down”);  
> posY = posY+chang;  
> branch\_mc.lineTo(posX, posY);  
> } else if (oldVal\>newVal && posY\>0) {  
> trace(“up”);  
> posY = posY-chang;  
> branch\_mc.lineTo(posX, posY);  
> }  
> return newVal;  
> };  
> this.watch(“xdir”, checkX);  
> this.watch(“ydir”, checkY);  
> this.onMouseMove = function() {  
> xdir = \_xmouse;  
> ydir = \_ymouse;  
> };  
> this.onEnterFrame = function() {  
> this.attachMovie(“ball”, “ball”, 200);  
> this.ball.\_x = posX;  
> this.ball.\_y = posY;  
> };

if u want to use this script by copying it, u need to create a movieclip(a circle or a rectangle) name it ball, and make it at properties export to actionScript

---

<div class="post-metadata">

### Author: ![cr125rider](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/cr125rider/32/534_2.png) [@cr125rider](https://forum.kirupa.com/u/cr125rider)
#### Post date: [April 19, 2006, 10:09pm UTC](https://forum.kirupa.com/t/direction-detection/186203/2 "2006-04-19T22:09:02Z")

</div>

an interesting effect, nice.

---

<div class="post-metadata">

### Author: ![kritikal](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kritikal/32/1699_2.png) [@kritikal](https://forum.kirupa.com/u/kritikal)
#### Post date: [April 19, 2006, 11:41pm UTC](https://forum.kirupa.com/t/direction-detection/186203/3 "2006-04-19T23:41:55Z")

</div>

very cool, you can make drawing competitions with this 😛 only annoying bug is that the square isn’t where the mouse. It gets really annoying when you think your mouse is where the square is but is in fact somewhere else…

---

<div class="post-metadata">

### Author: ![J11](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@J11](https://forum.kirupa.com/u/J11)
#### Post date: [April 20, 2006, 6:02am UTC](https://forum.kirupa.com/t/direction-detection/186203/4 "2006-04-20T06:02:19Z")

</div>

its nice 🙂  
but as ^ said the box doesnot exactly match movements of the mouse

---

<div class="post-metadata">

### Author: ![Exos\_Sho](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Exos\_Sho](https://forum.kirupa.com/u/Exos_Sho)
#### Post date: [April 20, 2006, 7:35am UTC](https://forum.kirupa.com/t/direction-detection/186203/5 "2006-04-20T07:35:26Z")

</div>

that’s on purpouse  
for each movement of the mouse the square moves 10 pixel  
it’s indirect proportional

---

<div class="post-metadata">

### Author: ![vini](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@vini](https://forum.kirupa.com/u/vini)
#### Post date: [April 24, 2006, 4:01am UTC](https://forum.kirupa.com/t/direction-detection/186203/6 "2006-04-24T04:01:22Z")

</div>

good one
