Return to Snippet

Revision: 12479
at March 16, 2009 13:05 by allnatural


Initial Code
//
//
// Initial variables.
var pendX = 250;
var pendY = 250;
var pendRadius = 200;
var pendArc = 135/360;
var pendSpeed = .005;
var pendCount = 0;
//
//
//
// Make a red footbal-shaped clip.
createEmptyMovieClip("Pendu", 10);
with(Pendu){
	beginFill(0xFF0000);
	moveTo(-20, 0);
	curveTo(0, 15, 20, 0);
	curveTo(0, -15, -20, 0);
}
//
// Make a clip in which to draw the stick holding the pendulum.
createEmptyMovieClip("Stick", 5);
//
//
//
// Swing a point on an eased arc with the given properties.
function pendulum (centerX, centerY, radius, aoi, completionRatio){
	//
	// Use magic trig function to turn linear values between 0
	// and 1 to eased values between -1 and +1.
	var easedOneToNegOne = Math.cos(completionRatio*2*Math.PI);
	//
	// Convert "Angle Of Inclination" from a ratio between 0
	// and 1 to radians.
	var aoiRadians = aoi * 2 * Math.PI;
	//
	// Determine eased rotation of the pendulum in radians.
	// Derived from #3 in the above "Conventions" section.
	var currentRotation = easedOneToNegOne * aoiRadians;
	//
	// Get X and Y coordinates of pendulum at eased angle of rotation.
	var x = centerX + Math.sin(currentRotation) * radius;
	var y = centerY + Math.cos(currentRotation) * radius;
	//
	// Return both X and Y coordinates inside a single 'point' object.
	return {x:x, y:y};
}
//
//
//
// Advance the pendulum to its next position.
function swingPendulum (){
	//
	// Increment the clock counter.
	pendCount += pendSpeed;
	pendCount %= 1;
	//
	// Get the pendulum's new coordinates.
	var point = pendulum (pendX, pendY, pendRadius, pendArc, pendCount);
	//
	// Place the pendulum at its new coordinates.
	Pendu._x = point.x;
	Pendu._y = point.y;
	//
	// Draw a line from the center to the pendulum.
	with (Stick){
		clear();
		lineStyle(2, 0);
		moveTo(pendX, pendY);
		lineTo(point.x, point.y);
	}
}
//
// Keep Swinging the pendulum every time we enter a frame.
onEnterFrame = swingPendulum;
//
//
//
//

Initial URL
http://www.pixelwit.com/blog

Initial Description


Initial Title
Swinging Pendulum

Initial Tags


Initial Language
ActionScript 3