Mouse attraction
Code Actionscript 2.0
Instance action
onClipEvent (load) {
startx = this._x;
starty = this._y;
mindistance = 160.0;
}
onClipEvent (enterFrame) {
softness_mouse = 4.0;
softness_start = 6.0;
dx = this._x - _root._xmouse;
dy = this._y - _root._ymouse;
distance = Math.sqrt(dx * dx + dy * dy);
if (distance < mindistance) {
//slide towards mouse
this._x = this._x - dx / softness_mouse;
this._y = this._y - dy / softness_mouse;
} else {
// slide back to starting point
this._x = this._x + (startx - this._x) / softness_start;
this._y = this._y + (starty - this._y) / softness_start;
}
}
Code Actionscript 3.0
addEventListener(Event.ENTER_FRAME, enterFrame); var startx = square.x; var starty = square.y; var mindistance = 160.0; function enterFrame(event:Event){ var softness_mouse = 4; var softness_start = 6; var dx = square.x - mouseX; var dy = square.y - mouseY; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < mindistance) { //slide towards mouse square.x = square.x - dx/ softness_mouse; square.y = square.y - dy/ softness_mouse; } else { // slide back to starting point square.x = square.x + (startx -square.x) /softness_start; square.y = square.y + (starty - square.y) / softness_start; } }
Description
The square is attracted to the mouse, following its path until the minimum distance mindistance is reached, at which point the square returns to its original position. The smoothness of these two movements is determined by the variables softness_mouse and softness_start.
related to: Movement towards a target, Mouse approach
Download
Right click: Flashfile AS 2.0 | Flashfile AS 3.0 | SWF-File