# Moving object in a straight line using mouse

**URL:** <https://forum.kirupa.com/t/moving-object-in-a-straight-line-using-mouse/254444>\
**Category:** Uncategorized\
**Created:** [March 12, 2008, 7:46am UTC](https://forum.kirupa.com/t/moving-object-in-a-straight-line-using-mouse/254444 "2008-03-12T07:46:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyenstyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@Eyenstyn](https://forum.kirupa.com/u/Eyenstyn)\
**Post date:** [March 12, 2008, 7:46am UTC](https://forum.kirupa.com/t/moving-object-in-a-straight-line-using-mouse/254444/1 "2008-03-12T07:46:39Z")

</div>

[LEFT]Hi, i have created some code to move an object to a mouse pointer’s position. The problem is that it does not travel in a straight line.

I think the problem has to do with the conditions of x and y. I want to calculate the gradient(x/y or (x1-x2)/(y1-y20) or create an incrementation of a set of points that are similar to the gradient to reach the destination…i dont know how to do that tho.

```auto
var circle:Sprite = new Sprite;
circle.graphics.lineStyle(1,0x0000FF);
circle.graphics.drawCircle(100,100,50);
circle.graphics.endFill();

var mouseClickCheck:Boolean = false;
var circleX:Number = 0;
var circleY:Number = 0;
stage.addEventListener(MouseEvent.CLICK, handleMouseMove);
addEventListener(Event.ENTER_FRAME, UpdatePosition);

addChild(circle);

function UpdatePosition(event:Event):void
{
    if(mouseClickCheck)
    {
        trace("[circleX] = " + circleX);
        trace("[circle.x] = " + circle.x);
            if(circle.x <= circleX)
            {
                circle.x += 100;
                if(circle.x > circleX)
                {
                    circle.x = circleX;
                }
            }
            if(circle.x >= circleX)
            {
                circle.x -= 100;
            }
            if(circle.y <= circleY)
            {
                circle.y += 100;
                if(circle.y > circleY)
                {
                    circle.y = circleY;
                }
            }
            if(circle.y >= circleY)
            {
                circle.y -= 100;
            }
    }
    
}

function handleMouseMove(event:MouseEvent):void
{
    mouseClickCheck = true;

    circleX = mouseX;
    circleY = mouseY;
    
    var g1:Sprite = new Sprite();
    g1.graphics.lineStyle(2,0x0000FF);
    g1.graphics.beginFill(0x0000FF);
    g1.graphics.drawCircle(mouseX,mouseY,5);
    addChild(g1);
}

```

[/LEFT]
