# Speed in relation to distance

**URL:** <https://forum.kirupa.com/t/speed-in-relation-to-distance/237252>\
**Category:** Uncategorized\
**Created:** [August 28, 2007, 5:31pm UTC](https://forum.kirupa.com/t/speed-in-relation-to-distance/237252 "2007-08-28T17:31:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![swampdonkey](https://avatars.discourse-cdn.com/v4/letter/s/df705f/32.png) [@swampdonkey](https://forum.kirupa.com/u/swampdonkey)\
**Post date:** [August 28, 2007, 5:31pm UTC](https://forum.kirupa.com/t/speed-in-relation-to-distance/237252/1 "2007-08-28T17:31:32Z")

</div>

Hi,

Is there a way i can make a movieclip speed up or slow down according to the distance between the mouse and the movieclip? I’ve made a friction effect for a movie clip (script 1) and i have a script to work out the distance between the mouse and an object (script 2) but i cant seem to integrate the two…anyone done this? I dont understand the distance script 100%, i pretty much got it from a book i own…any help on this would be great…

script 1 - friction.

```auto

xmov = 50;
decay = .99;
this.onEnterFrame = function() {
    if (_root._xmouse > Stage.width/2) {
        this.cam.x += xmov;
    }
}

```

script 2 - distance

```auto

var mc1 = largest_mc;
onEnterFrame = function() {
var dx:Number = mc1._x - _xmouse;
var dy:Number = mc1._y - _ymouse;
var dist:Number = Math.round(Math.sqrt((dx*dx) + (dy*dy)));
trace(dist);

}

```

Thanks…
