var mouseSpeedX:Number = 0;
var prevX:Number = 0;
var pends:Array = new Array();
for (var i:int = 0; i<10; i++){
pends.push(makePendulum(100+ i * 40, 100, 15, 100 + i * 10));
}
addEventListener(Event.ENTER_FRAME, onRun);
function onRun(evt:Event):void {
// mouseSpeed
mouseSpeedX = prevX - mouseX;
prevX = mouseX;
for (var i:int = 0; i<pends.length; i++) pends[i]();
}
function makePendulum(xp:Number, yp:Number, rad:Number, leng:Number):Function {
var rot:Number = 0;
var rotDest :Number = 0;
var rotVel:Number = 0
var string:Shape = Shape(addChild(new Shape()));
var ball:Sprite = Sprite(addChild(new Sprite()));
ball.buttonMode = true;
with(ball.graphics) beginFill(0xFF0000), drawCircle(0,leng, rad);
ball.x = xp;
ball.y = yp;
string.x = ball.x;
string.y = ball.y;
ball.addEventListener(MouseEvent.ROLL_OVER,function(){
rotDest = mouseSpeedX;
});
return function(){
// force rotDest back to 0
rotDest *= .8;
// elasticity (hooke's)
rotVel += (-1.9 * (rot - rotDest) - rotVel) / 4;
rot += rotVel
ball.rotation = rot;
// draw string:
string.graphics.clear();
string.graphics.lineStyle(0,0);
var pnt:Point = ball.localToGlobal(new Point(0, leng))
string.graphics.curveTo(0, leng / 2, pnt.x - ball.x, pnt.y-ball.y);
}
}
This is a variation on something I wrote in response to a student question. It creates a few pendulums that can be pushed with the mouse.
just discovered this site – very cool stuff and extremely useful; very cool pendulum experiment btw…
Thanks sam… glad you like the site.
amazing snippets, really useful. Thanks and keep up the good work ๐
Thanks patrick…
Thanks for posting this! I’m still trying to get my tags working from when I first asked you in class. It’s helping a lot!