gravity:Star
// forked from yo0_0oy's gravity
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import com.flashdynamix.utils.SWFProfiler;
[SWF(backgroundColor="#000000", frameRate="60", width="465", height="465")]
public class Sample extends Sprite
{
private var numParticles:Number = 100;
private var particles:Array;
private static const MAX_SIZE:Number = 30;
public function Sample()
{
init();
SWFProfiler.init( this );
}
/*
* オブジェクトを追加して場所,大きさをランダムで初期化
*/
private function init():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
particles = new Array();
for(var i:uint = 0; i < numParticles; i++)
{
var size:Number = Math.random() * MAX_SIZE;
var particle:Star = new Star(size);
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
particle.mass = size % 5+1;
addChild(particle);
particles.push(particle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
for(var i:uint = 0; i < numParticles; i++)
{
var particle:Star = particles[i];
particle.x += particle.vx;
particle.y += particle.vy;
}
for(i=0; i < numParticles - 1; i++)
{
var partA:Star = particles[i];
for(var j:uint = i + 1; j < numParticles; j++)
{
var partB:Star = particles[j];
gravitate(partA, partB);
}
}
}
private function gravitate(partA:Star, partB:Star):void
{
var dx:Number = partB.x - partA.x;
var dy:Number = partB.y - partA.y;
var distSQ:Number = dx*dx + dy*dy;
var dist:Number = Math.sqrt(distSQ);
var force:Number = partA.mass * partB.mass / distSQ;
var ax:Number = force * dx / dist;
var ay:Number = force * dy / dist;
partA.vx += ax / partA.mass;
partA.vy += ay / partA.mass;
partB.vx -= ax / partB.mass;
partB.vy -= ay / partB.mass;
}
}
}
import flash.display.Sprite;
import flash.filters.GlowFilter;
internal class Star extends Sprite
{
private var _color:uint;
private var _radius:Number;
public var mass:Number = 1;
public var vx:Number = 0;
public var vy:Number = 0;
public function Star(radius:Number=20,color:uint=0xffffff)
{
_color = color;
_radius = radius;
draw();
}
private function draw():void {
graphics.lineStyle(0);
graphics.moveTo(_radius,0);
graphics.beginFill(_color);
this.filters = [new GlowFilter(0xffff00)];
for(var i:int = 1; i<=10 ;i++){
var radius:Number = _radius;
if(i%2>0){
radius = _radius/2;
}
var angle:Number = Math.PI*2/10*i;
graphics.lineTo(Math.cos(angle)*radius,Math.sin(angle)*radius);
}
}
}