/*************************************************************************************************************** Vector2D Class - Quick And Dirty Sample Source --------------------------------------------------------------------------------------------------------------- This sample source is the code for demonstration number three 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" Mouse.hide(); // set relocated xmouse and ymouse _global.mouseX = _xmouse; _global.mouseY = _ymouse; // create body movieclip this.createEmptyMovieClip("body", 1); with (body) { moveTo(-30, -30); beginFill(0xCC0033, 100); lineTo(30, -30); lineTo(30, 30); lineTo(-30, 30); lineTo(-30, -30); endFill(); } // set up vectors body.positionVector = new Vector2D(200, 200); body.velocityVector = new Vector2D(0, 0); body.accelVector = new Vector2D(0, 0); body.gravityVector = new Vector2D(0, .0009); body.dragging = false; body.onEnterFrame = function() { if (body.dragging) { // eased follow on drag body.positionVector.x += (mouseX - body._x) * 0.2; body.positionVector.y += (mouseY - body._y) * 0.2; } else { // apply accel and gravity with (body) { accelVector.plus(gravityVector); velocityVector.plus(accelVector); } // calculate and apply friction var frictionVector = new Vector2D(body.velocityVector); frictionVector.invert(); frictionVector.times(0.007); body.velocityVector.plus(frictionVector); } var nextX = body.positionVector.x + body.velocityVector.x; with (body) { // collision response for walls if (nextX > 530) { body.positionVector.x = 530; body.velocityVector.x = body.velocityVector.x * -1; } if (nextX < 30) { body.positionVector.x = 30; body.velocityVector.x = body.velocityVector.x * -1; } if ((positionVector.y + velocityVector.y) > 350) { positionVector.y = 350; velocityVector.y = 0; accelVector.set(0, 0); } // update position _x = positionVector.x += velocityVector.x; _y = positionVector.y += velocityVector.y; } }; // make arm this.createEmptyMovieClip("arm", 2); this.arm._x = Stage.width / 2; this.arm._y = Stage.height / 2; this.arm.onEnterFrame = function() { // set mouseX and mouseY mouseX += (_xmouse - mouseX) * 0.5; mouseY += (_ymouse - mouseY) * 0.5; // draw lines var point = new Object(); point.x = mouseX; point.y = mouseY; arm.globalToLocal(point); arm.clear(); arm.moveTo(0, 0); arm.lineStyle(0, 0x33CC66, 100); arm.lineTo(point.x, point.y); point.x = body._x; point.y = body._y; arm.globalToLocal(point); arm.lineStyle(0, 0xFFFF00, 100); arm.lineTo(point.x, point.y); }; // set click events this.onMouseDown = function() { body.dragging = true; // initialize vel and accel since dragging body.velocityVector.set(0, 0); body.accelVector(0, 0); }; this.onMouseUp = function() { body.dragging = false; // set throw velocity body.velocityVector.set(0.1 * (_root._xmouse - body._x), 0.1 * (_root._ymouse - body._y)); };