バネの動きの練習
package {
import flash.display.Sprite;
import flash.events.Event;
[SWF(width="300", height="300", backgroundColor="0x000000", frameRate="30")]
public class Spring extends Sprite {
private var ball:Ball;
private var friction:Number =0.9
private var spring:Number = 0.8
private var springLength = 100;
private var ballNum:uint = 100;
private var ballArray:Array = new Array;
public function Spring() {
stage.quality = "low";
for (var i:uint = 0; i < ballNum; i++) {
var radius:Number = Math.random() * 30 + 30;
ball = new Ball(radius);
addChild(ball);
ballArray.push(ball)
ball.x = Math.random() * stage.stageWidth;
ball.y = Math.random()*stage.stageHeight;
}
addEventListener(Event.ENTER_FRAME, xEnterFrame);
}
private function xEnterFrame(e:Event):void {
for ( var i:uint = 0; i < ballNum; i++) {
var ball = ballArray[i]
var dx:Number = ball.x - mouseX;
var dy:Number = ball.y - mouseY;
var angle:Number = Math.atan2(dy,dx);
var targetX:Number = mouseX + Math.cos(angle) * springLength;
var targetY:Number = mouseY + Math.sin(angle) * springLength;
var ax = (targetX - ball.x) * spring
ball.vx += ax;
ball.vx *= friction;
ball.x += ball.vx;
var ay = (targetY - ball.y) * spring
ball.vy += ay
ball.vy *= friction;
ball.y += ball.vy
}
}
}
}
import flash.display.Shape;
import flash.filters.BlurFilter;
class Ball extends Shape {
public var vx:Number = 0;
public var vy:Number = 0;
function Ball(radius:Number = 20) {
var color:Number = Math.random() * 0xFFFFFF;
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
blendMode = "add"
var blur:BlurFilter = new BlurFilter(10,10,1);
filters = [blur];
alpha = 0.5
}
}