Mouse avoiding
Code Actionscript 2.0
Instance action
onClipEvent (load) {
startx = this._x;
starty = this._y;
mindistance = 80.0;
}
onClipEvent (enterFrame) {
smoothness = 12.0;
dx = this._x - _root._xmouse;
dy = this._y - _root._ymouse;
distance = Math.sqrt(dx * dx + dy * dy);
if (distance < mindistance) {
// move away from mouse
this._x = _root._xmouse + (dx * mindistance / distance);
this._y = _root._ymouse + (dy * mindistance / distance);
} else {
// move back to starting point
this._x = this._x + (startx - this._x) / smoothness;
this._y = this._y + (starty - this._y) / smoothness;
}
}
Code Actionscript 3.0
addEventListener(Event.ENTER_FRAME, enterFrame); var startx = square.x; var starty = square.y; var mindistance = 80.0; function enterFrame(evet:Event) { var smoothness = 12.0; var dx = square.x - mouseX; var dy = square.y - mouseY; var distance = Math.sqrt(dx * dx + dy * dy); // move away from mouse if (distance < mindistance) { square.x = mouseX + (dx * mindistance / distance); square.y = mouseY + (dy * mindistance / distance); } else { // move back to starting point square.x = square.x + (startx - square.x) / smoothness; square.y = square.y + (starty - square.y) / smoothness; } }
Description
The square tries to avoid the mouse, once it gets closer than mindistance. If that distance is lost, the square will return to its original position.
Download
Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File