# Balls that follow my drift

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?

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

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:

``````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:

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

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

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?