lots of balls
forked from blue ball (diff: 72)
ActionScript3 source code
/**
* Copyright wexler ( http://wonderfl.net/user/wexler )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fWCk
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.*;
import flash.utils.getTimer;
import net.hires.debug.Stats;
[SWF(width = "400", height = "400", frameRate = "60", backgroundColor = "#7F7F7F")]
public class mySprite extends Sprite {
private const _size:uint = 500;
private const _n_dots:uint = 500;
private const _r_min:Number = 0.475;
private const _r_max:Number = 0.95;
private const _radius:uint = 7;
private const _speed:Number = (10*Math.PI/180)/1000.0; // in radians/msec
//private const _frames:uint = 2;
private const _fp_radius:uint = 4;
private var _dots:Array = new Array(_n_dots);
private var _stats:* = undefined;
public function mySprite() {
randomize();
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_listener);
}
public function mouse_listener(e:MouseEvent):void
{
if(!e.shiftKey) {
randomize();
}
else {
if(_stats) {
removeChild(_stats);
_stats = undefined;
}
else {
_stats = addChild(new Stats());
}
}
}
private function randomize():void {
const z:Number = Math.pow(_r_min/_r_max, 2);
const u:Number = 1 - z;
for(var i:uint = 0; i < _n_dots; i++) {
var r:Number = Math.sqrt(z + u*Math.random())*_r_max;
var th:Number = Math.random()*2*Math.PI;
var gray:uint = Math.random()*256;
_dots[i] = new dot(r, th, gray + 256*gray + 65536*gray);
}
}
public function update(e:Event):void
{
const theta0:Number = getTimer()*_speed;
graphics.clear();
for each(var p:dot in _dots) {
const x:Number = (1 + p.r*Math.cos(p.theta + theta0))*_size/2;
const y:Number = (1 - p.r*Math.sin(p.theta + theta0))*_size/2;
this.graphics.beginFill(p.color);
this.graphics.drawCircle(x, y, _radius);
graphics.endFill();
}
this.graphics.beginFill(0xFF0000);
this.graphics.drawCircle(_size/2, _size/2, _fp_radius);
graphics.endFill();
}
}
}
class dot
{
public var r:Number;
public var theta:Number;
public var color:uint;
public function dot(R:Number, Theta:Number, Color:uint)
{
r = R;
theta = Theta,
color = Color;
}
}