# MoveToPoint function

**URL:** https://forum.kirupa.com/t/movetopoint-function/279699
**Category:** flash
**Created:** [January 17, 2009, 7:51pm UTC](https://forum.kirupa.com/t/movetopoint-function/279699 "2009-01-17T19:51:46Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![WoutervD](https://avatars.discourse-cdn.com/v4/letter/w/34f0e0/32.png) [@WoutervD](https://forum.kirupa.com/u/WoutervD)
#### Post date: [January 17, 2009, 7:51pm UTC](https://forum.kirupa.com/t/movetopoint-function/279699/1 "2009-01-17T19:51:46Z")

</div>

Hello All!

I am currently trying to write a good function that moves and object in 2D space to a specific point (x, y).

The function is as follows:

```auto

public function moveTowards(pX, pY):void {
    var dir:int = Math.atan2(pY-y,pX-x)*180/Math.PI;
    x += Math.round(Math.cos(dir*Math.PI/180)*iSpeed);
    y += Math.round(Math.sin(dir*Math.PI/180)*iSpeed);
}

```

The method is part of a class that is extend from the MovieClip class.

pX and pY are the coordinates to the position where the object has to move to.  
iSpeed is a variable that defines the object its speed, for example 5.

The problem with this function is that the destination will never be reached.  
There is always a slight difference from the current position to the destination position, resulting in a shaking/vibrating object. The position seems to oscillating or something.

How can I solve this?

What I want is that the object moves in a straight line to the destination. So the x and y speeds are unequal unless both distances are the same…
