package ; import flash.display.Shape; import flash.events.TimerEvent; import flash.Lib; import flash.Vector; import flash.utils.Timer; class Particle { var x0 : Float; var y0 : Float; var t0 : Float; var vx : Float; var vy : Float; var shape : Shape; static var velocity : Float = 100; public function new(t : Float) { shape = new Shape(); var clr = Std.int(Math.random() * 180 + 70); shape.graphics.beginFill(clr); shape.graphics.drawCircle(0, 0, 2.5); shape.graphics.endFill(); reinit(t); shape.x = x0; shape.y = y0; Lib.current.addChild(shape); } function reinit(t : Float):Void { var angle = Math.random() * 2 * Math.PI; vy = Math.sin(angle) * velocity; vx = Math.cos(angle) * velocity; t0 = t; x0 = Math.random() * Main.X; y0 = Math.random() * Main.Y; } public function move(t : Float):Void { var dt = t - t0; if (dt > 2.5 && Math.random() < 0.004) reinit(t); var dx = Math.sin(dt*6) * 2; var dy = Math.cos(dt*6) * 2; var x = x0 + (t - t0) * vx + dx; var y = y0 + (t - t0) * vy + dy; shape.x = x; shape.y = y; if (x < 0 || y < 0 || x > Main.X || y > Main.Y) reinit(t); } } class Main { public static inline var X = 500; public static inline var Y = 360; static inline var D = 80; static var N = 250; var particles : Vector; var timer : Timer; static function main() { var m = new Main(); } public function new() { particles = new Vector(); var t0 = haxe.Timer.stamp(); for (n in 0...N) particles.push(new Particle(t0)); add_marker(X / 2 - D, Y / 2 - D); add_marker(X / 2 + D, Y / 2 - D); add_marker(X / 2 - D, Y / 2 + D); add_marker(X / 2 + D, Y / 2 + D); timer = new Timer(10); timer.addEventListener(TimerEvent.TIMER, on_timer); timer.start(); } function add_marker(x : Float, y : Float):Void { var shape = new Shape(); shape.graphics.beginFill(0xcFcF00); shape.graphics.drawCircle(0, 0, 4); shape.graphics.endFill(); shape.x = x; shape.y = y; Lib.current.addChild(shape); } public function on_timer(e:TimerEvent):Void { var t = haxe.Timer.stamp(); for (p in particles) p.move(t); } }