multiframe
forked from lots of balls (diff: 67)
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/M7lE
*/
// forked from wexler's lots of balls
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 = "500", height = "500", frameRate = "60", backgroundColor = "#7F7F7F")]
public class main 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 = 3;
private const _fp_radius:uint = 4;
private var _dots:Array = new Array(_frames);
private var _in_noise:Boolean = false;
private var _frame:uint = 0;
private var _frame_dir:int = -1;
private var _stats:* = undefined;
public function main()
{
randomize();
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse_listener);
}
public function update(e:Event):void
{
if(_in_noise) {
if(_frame_dir == +1) {
if(_frame < _frames - 1) {
_frame++;
}
else {
_frame = _frames - 1;
_in_noise = false;
}
}
else {
if(_frame > 0) {
_frame--;
}
else {
_frame = 0;
_in_noise = false;
}
}
}
const theta0:Number = getTimer()*_speed;
graphics.clear();
for each(var p:dot in _dots[_frame]) {
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();
}
public function mouse_listener(e:MouseEvent):void
{
if(!e.shiftKey) {
_in_noise = true;
_frame_dir *= -1;
}
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 f:uint = 0; f < _frames; f++) {
_dots[f] = new Array(_n_dots);
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[f][i] = new dot(r, th, gray + 256*gray + 65536*gray);
}
}
}
}
}
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;
}
}