Rainbow Worms Phlashers 2009 AS3 Contest http://www.phlashers.com creek23 forked:0favorite:1lines:118license : MIT License modified : 2009-12-28 23:05:46 Embed Tweet package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; /** * Phlashers 2009 AS3 Contest * http://www.phlashers.com */ [SWF(width=465, height=465, frameRate=60, backgroundColor=0x000000)] public class M extends Sprite { private var rSM:SM private var oSM:SM private var ySM:SM private var gSM:SM private var bSM:SM private var vSM:SM public function M():void { addEventListener(Event.ADDED_TO_STAGE, init) } public static function Mr():Number { return Math.random() } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init) rSM = new SM(graphics) oSM = new SM(graphics) ySM = new SM(graphics) gSM = new SM(graphics) bSM = new SM(graphics) vSM = new SM(graphics) var rS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) var oS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) var yS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) var gS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) var bS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) var vS:S = new S(new Point(Mr() * stage.stageWidth, Mr() * stage.stageHeight)) rSM.aS(rS); oSM.aS(oS); ySM.aS(yS); gSM.aS(gS); bSM.aS(bS); vSM.aS(vS) var i:uint for (i = 0; i < 35; i++) { rS = new S(new Point(rS.p.x + (Mr() * 5 + 1), rS.p.y + (Mr() * 5) + 1), rS) rS.clr = 0xFF0000; rSM.aS(rS) oS = new S(new Point(oS.p.x + (Mr() * 5 + 1), oS.p.y + (Mr() * 5) + 1), oS) oS.clr = 0xFF9900; oSM.aS(oS) yS = new S(new Point(yS.p.x + (Mr() * 5 + 1), yS.p.y + (Mr() * 5) + 1), yS) yS.clr = 0xFFFF00; ySM.aS(yS) gS = new S(new Point(gS.p.x + (Mr() * 5 + 1), gS.p.y + (Mr() * 5) + 1), gS) gS.clr = 0x66FF66; gSM.aS(gS) bS = new S(new Point(bS.p.x + (Mr() * 5 + 1), bS.p.y + (Mr() * 5) + 1), bS) bS.clr = 0x0000FF; bSM.aS(bS) vS = new S(new Point(vS.p.x + (Mr() * 5 + 1), vS.p.y + (Mr() * 5) + 1), vS) vS.clr = 0xCC00CC; vSM.aS(vS) } stage.addEventListener(MouseEvent.MOUSE_MOVE, stage_onMouseMove) stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame) } private function stage_onEnterFrame(e:Event):void { this.graphics.clear() rSM.r(); oSM.r(); ySM.r(); gSM.r(); bSM.r(); vSM.r() rSM.fS.p = new Point((Mr() * 20 - 10) + rSM.fS.p.x, (Mr() * 20 - 10) + rSM.fS.p.y) oSM.fS.p = new Point((Mr() * 20 - 10) + oSM.fS.p.x, (Mr() * 20 - 10) + oSM.fS.p.y) ySM.fS.p = new Point((Mr() * 20 - 10) + ySM.fS.p.x, (Mr() * 20 - 10) + ySM.fS.p.y) gSM.fS.p = new Point((Mr() * 20 - 10) + gSM.fS.p.x, (Mr() * 20 - 10) + gSM.fS.p.y) bSM.fS.p = new Point((Mr() * 20 - 10) + bSM.fS.p.x, (Mr() * 20 - 10) + bSM.fS.p.y) vSM.fS.p = new Point((Mr() * 20 - 10) + vSM.fS.p.x, (Mr() * 20 - 10) + vSM.fS.p.y) } private function stage_onMouseMove(e:MouseEvent):void { var tP:Point = new Point(e.target.mouseX, e.target.mouseY) rSM.fS.p = S.getPt(tP, rSM.fS.p) oSM.fS.p = S.getPt(tP, oSM.fS.p) ySM.fS.p = S.getPt(tP, ySM.fS.p) gSM.fS.p = S.getPt(tP, gSM.fS.p) bSM.fS.p = S.getPt(tP, bSM.fS.p) vSM.fS.p = S.getPt(tP, vSM.fS.p) } } } import flash.display.Graphics import flash.geom.Point class S { private var _p:Point public var s:S public var a:Number public var clr:uint public function S(p_p:Point, p_s:S = null) { p = p_p; s = p_s; a = 1 } public function get p():Point { return _p } public function set p(v:Point):void { _p = new Point(v.x, v.y) } public function r(c:Graphics):void { c.lineStyle(M.Mr() * 3, clr, a) if (s) { c.moveTo(s.p.x, s.p.y) p = getPt(s.p, p) } else { c.moveTo(p.x, p.y) } c.lineTo(p.x, p.y) } public static function getPt(pt:Point, pc:Point):Point { var dp:Point = Point.polar(M.Mr() * Point.distance(pt, pc) + 1, Math.atan2(pc.y - pt.y, pc.x - pt.x)) return new Point(dp.x + pt.x + ((M.Mr() * 10 > 5) ? 0.0 : -0.0), dp.y + pt.y) } } import flash.display.Graphics class SM { public var sL:Array public var c:Graphics public var a:Number; public function SM(pc:Graphics) { sL = new Array() c = pc; a = 1; } public function aS(ps:S):void { ps.a = a; a-=0.025; sL.push(ps) } public function r():void { for (var i:int = 0; i < sL.length; i++) { S(sL[i]).r(c) } } public function get lS():S { return (sL.length > 0) ? S(sL[sL.length-1]) : null } public function get fS():S { return (sL.length > 0) ? S(sL[0]) : null } } Code Fullscreen Preview Fullscreen devm 2009 as3 contest phlashers Point MouseEvent.ADDED_TO_STAGE Point.polar length moveTo Point.distance MouseEvent.MOUSE_MOVE clear Math.atan2 MouseEvent.ENTER_FRAME lineTo lineStyle MouseEvent push Array Math.random uint int Number