# Balls that follow my drift

**URL:** <https://forum.kirupa.com/t/balls-that-follow-my-drift/5668>\
**Category:** Uncategorized\
**Created:** [September 19, 2002, 2:28am UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668 "2002-09-19T02:28:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbzfan33](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@dbzfan33](https://forum.kirupa.com/u/dbzfan33)\
**Post date:** [September 19, 2002, 2:28am UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/1 "2002-09-19T02:28:17Z")

</div>

I’m making a fun fun fun site and i want to add thjis cool cool cool effect. ok ok ok, my navigation bar is like horizontal aligned, and i drew a ball and made it a movie clip and put it over the navigation bar… i want the ball to follow the mouse when you move it to the right or left, but never move up or down… i have an idea what the code would be like. i think it’s something like…

funball\_y or \_x = mouse\_y or _x… i forgot my x and y coordinates ^_^;. but i wanna know what the code will be. i have no idea! Can someone please help?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2002, 5:33am UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/2 "2002-09-19T05:33:09Z")

</div>

here is some code to do something like that, with an elastic effect as well, you can adjust the vars at the begining to play with its movement. Also your ball has to have the instance name of ball\_mc… or modify the code for whatever instance you want to use. Here you go:

var acceleration = 10;  
var friction = .80;

ball\_mc.moveToMouse = function() {

```
var xdif=this.targetX-this._x;
this.xspeed += xdif/this._parent.acceleration;
this.xspeed *= this._parent.friction;
this._x += this.xspeed;
this.checkDistance();

```

};

ball\_mc.checkDistance = function() {  
if (Math.abs(this.targetX-this.\_x)\<0.2) {  
this.\_x = this.targetX;  
}  
}

ball\_mc.onMouseMove = function() {  
this.targetX = this.\_parent.\_xmouse;  
this.onEnterFrame = this.moveToMouse;  
};

Peace

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2002, 12:59pm UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/3 "2002-09-19T12:59:22Z")

</div>

You’re right. To make the ball follow perfectly, you have to assign the \_x position of the ball to the position of the mouse:

```auto
onClipEvent (enterFrame) {
     _x=_root._xmouse;
}

```

Then you can try the easing tutorial to make it a bit smoother. And finally the springs tutorial or Ryall’s code for an elastic effect (everything on this site).

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2002, 9:38pm UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/4 "2002-09-19T21:38:49Z")

</div>

Ok, i tried the second reply and tha works, but… how do i set spots so it can’t pass a certain point?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2002, 11:26pm UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/5 "2002-09-19T23:26:03Z")

</div>

set your x max and min and create a condition that nulls your mouse movement if it is beyond your range

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 19, 2002, 11:53pm UTC](https://forum.kirupa.com/t/balls-that-follow-my-drift/5668/6 "2002-09-19T23:53:53Z")

</div>

o.O;;;  
Sorry…  
I’m not that smart with actionscript… so i don’t know what you mean… Or are you talking about on the code you gave me?
