/*************************************************************************************************************** Vector2D Class - Another Quick And Dirty Sample Source --------------------------------------------------------------------------------------------------------------- This sample source is the code for demonstration number four on the Vector2D website. To use this source: 1. Download the 'Vector2D.as' file 2. Create a new Flash Movie 3. Save the movie in the same folder as the 'Vector2D.as' file 4. Paste this code on frame 1 5. Publish If you are publishing for Flash Player 5 or 6: 1. Download the 'Vector2D_AS1.as' file 2. Create a new Flash Movie 3. Save the movie in the same folder as the 'Vector2D_AS1.as' file 4. Paste this code on frame 1 5. Uncomment first line of the code you pasted on frame 1 6. Publish You will most definitely want to set the frame rate much higher than the default 12fps ***************************************************************************************************************/ // uncomment the following line for Actionscript 1.0 //#include "Vector2D_AS1.as" // create mouse position vector that updates every frame _global.mousePosition = new Vector2D(_xmouse, _ymouse); this.onEnterFrame = function() { _global.mousePosition.set(_xmouse, _ymouse); }; // body creation function var bodyCount = 0; function createBody(xPosition, yPosition) { ++bodyCount; // create and draw body movieclip this.createEmptyMovieClip("body_" + bodyCount, bodyCount); var theBody = this["body_" + bodyCount]; with (theBody) { moveTo(-25, -25); beginFill(0xCC0033, 100); lineTo(25, -25); lineTo(25, 25); lineTo(-25, 25); lineTo(-25, -25); endFill(); } // record initial position theBody.positionInitial = new Vector2D(xPosition, yPosition); // calculate new position every frame theBody.onEnterFrame = function() { var positionOffset = new Vector2D(_global.mousePosition); positionOffset.minus(theBody.positionInitial); positionOffset.invert(); var distance = positionOffset.magnitude; if (distance < 100) { positionOffset.magnitude = (1 - (distance / 100)) * 75; } else { positionOffset.set(0, 0); } var positionCurrent = new Vector2D(theBody.positionInitial); positionCurrent.plus(positionOffset); theBody._x += (positionCurrent.x - theBody._x) * 0.1; theBody._y += (positionCurrent.y - theBody._y) * 0.1; }; } // create bodies for (var i = 0; i < 3; ++i) { for (var j = 0; j < 3; ++j) { createBody((i * 75)+205, (j * 75)+115); } }