Hello everybody, i have found a code whose purpose is to make an object to follow the mouse arrow. It works perfect, but i would like to add a kind of acceleration to it. That means the closer the object is to the mouse arrow, the faster it moves.
Could somebody find what can i add to this code to make it works?
thanks a lot in advance.
HERE THE CODE:
var Speed:Number = 1 // Velocidad del personaje
var TmpXY:Number = 0;
onEnterFrame = function() {
gotoSpotX = this._parent._xmouse;
gotoSpotY = this._parent._ymouse;
// calculate angle of current position to target position
delta_x = this._x-gotoSpotX;
delta_y = this._y-gotoSpotY;
targetRotation = -Math.atan2(delta_x, delta_y)/(Math.PI/180);
if (targetRotation<0){
targetRotation += 360//Acomoda el angulo para que sea de 360 grados
}
// move beetle toward the target and stop when it gets there
if (Math.sqrt((delta_x*delta_x)+(delta_y*delta_y))>Speed) {
_y -= Speed*Math.cos(targetRotation*(Math.PI/180));
_x += Speed*Math.sin(targetRotation*(Math.PI/180));
this.Fachadas.Parado = false; //Controla fachada de estar parado
}else{
this.Fachadas.Parado = true;
}
//
// loop to opposite side of the masked area when the beetle travels off-screen
if (_y<0) {
_y = 400;
}
if (_y>400) {
_y = 0;
}
if (_x<0) {
_x = 400;
}
if (_x>400) {
_x = 0;
}
// Asigna el cambio de fachadas
if (targetRotation<45){
this.Fachadas.Angulo = 0;
}else if (targetRotation<90){
this.Fachadas.Angulo = 45;
}else if (targetRotation<135){
this.Fachadas.Angulo = 90;
}else if (targetRotation<180){
this.Fachadas.Angulo = 135;
}else if (targetRotation<225){
this.Fachadas.Angulo = 180;
}else if (targetRotation<270){
this.Fachadas.Angulo = 225;
}else if (targetRotation<315){
this.Fachadas.Angulo = 270;
}else if (targetRotation<360){
this.Fachadas.Angulo = 0;
}