package { import flash.display.Sprite; import flash.display.LineScaleMode; import flash.events.MouseEvent; import flash.geom.PerspectiveProjection; import flash.geom.Utils3D; import flash.geom.Vector3D; import flash.geom.Matrix3D; import flash.geom.*; import flash.events.Event; import flash.utils.Dictionary; [SWF(backgroundColor="0x000000", width="800", height="600", frameRate="30")] public class Dandelion extends Sprite { private var vertices:Vector. = new Vector.(); private var projectedVerts:Vector. = new Vector.(); private var r:int = 0; private var spores:Vector. = new Vector.(); private var sporesDct:Dictionary = new Dictionary(true); private var blown:Boolean = false; public function Dandelion(){ //addEventListener(MouseEvent.MOUSE_UP, onMouseUp); var radius:Number = 70; for(var lat:Number= 0, long:Number= 0; lat < Math.PI; lat+=0.01,long+=0.5){ vertices.push( 10*Math.cos(long)*Math.sin(lat), 10*Math.cos(lat), 10*Math.sin(long)*Math.sin(lat), radius*Math.cos(long)*Math.sin(lat), radius*Math.cos(lat), radius*Math.sin(long)*Math.sin(lat), (radius + 10)*Math.cos(long-Math.random()*0.1)*Math.sin(lat-Math.random()*0.05), (radius + 10)*Math.cos(lat-Math.random()*0.05), (radius + 10)*Math.sin(long-Math.random()*0.1)*Math.sin(lat-Math.random()*0.05), (radius + 10)*Math.cos(long+Math.random()*0.1)*Math.sin(lat-Math.random()*0.05), (radius + 10)*Math.cos(lat-Math.random()*0.05), (radius + 10)*Math.sin(long+Math.random()*0.1)*Math.sin(lat-Math.random()*0.05), (radius + 10)*Math.cos(long+Math.random()*0.1)*Math.sin(lat+Math.random()*0.05), (radius + 10)*Math.cos(lat+Math.random()*0.05), (radius + 10)*Math.sin(long+Math.random()*0.1)*Math.sin(lat+Math.random()*0.05), (radius + 10)*Math.cos(long-Math.random()*0.1)*Math.sin(lat+Math.random()*0.05), (radius + 10)*Math.cos(lat+Math.random()*0.05), (radius + 10)*Math.sin(long-Math.random()*0.1)*Math.sin(lat+Math.random()*0.05) ); var spore:Sprite = new Sprite(); spore.x = stage.stageWidth/2; spore.y = stage.stageHeight/2; spores.push(spore); addChild(spore); sporesDct[spore] = { xp:radius*Math.cos(long)*Math.sin(lat), yp:radius*Math.cos(lat), zp:radius*Math.sin(long)*Math.sin(lat), firstVertex: vertices.length-18, lastVertex: vertices.length-1, blown: false, xv:Math.random()*2 - 1, yv:Math.random()*2 - 1, zv:Math.random()*2 - 1, xr:Math.random()*2 - 1, yr:Math.random()*2 - 1, zr:Math.random()*2 - 1 }; spore.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void { sporesDct[event.target]["blown"]=true; }); } addEventListener(Event.ENTER_FRAME, onEnterFrame); //onEnterFrame(); } /*private function onMouseUp(event:MouseEvent):void { spores[event.target]["blown"]=true; //blown=true; }*/ private function onEnterFrame(event:Event=null):void { var m:Matrix3D = new PerspectiveProjection().toMatrix3D(); m.prependTranslation(0,0,10); //move spores spores.forEach(function(spore:Sprite, ind:int, a:Vector.):void { var verts:Vector. = vertices.slice(sporesDct[spore].firstVertex,sporesDct[spore].lastVertex+1); var projVerts:Vector. = new Vector.(); var m2:Matrix3D = new Matrix3D(); if(sporesDct[spore].blown){ m2.appendTranslation(sporesDct[spore].xv,sporesDct[spore].yv,sporesDct[spore].zv); m2.appendRotation(sporesDct[spore].xr,Vector3D.X_AXIS); m2.appendRotation(sporesDct[spore].yr,Vector3D.Y_AXIS); m2.appendRotation(sporesDct[spore].zr,Vector3D.Z_AXIS); } m2.transformVectors(verts,projVerts); vertices.splice(sporesDct[spore].firstVertex,18, projVerts[0], projVerts[1], projVerts[2], projVerts[3], projVerts[4], projVerts[5], projVerts[6], projVerts[7], projVerts[8], projVerts[9], projVerts[10], projVerts[11], projVerts[12], projVerts[13], projVerts[14], projVerts[15], projVerts[16], projVerts[17]); }); Utils3D.projectVectors(m, vertices, projectedVerts, Vector.([])); for(var i:int = 0; i < projectedVerts.length; i+=12){ var spore:Sprite = spores[i/12]; spore.graphics.clear(); spore.graphics.lineStyle((100 - 1*sporesDct[spore].zp)/100,0xFFFFFF); spore.graphics.drawPath(Vector.([1,2,2,1,2,1,2,1,2]),Vector.([ projectedVerts[i],projectedVerts[i+1], projectedVerts[i+2],projectedVerts[i+3], projectedVerts[i+4],projectedVerts[i+5], projectedVerts[i+2],projectedVerts[i+3], projectedVerts[i+6],projectedVerts[i+7], projectedVerts[i+2],projectedVerts[i+3], projectedVerts[i+8],projectedVerts[i+9], projectedVerts[i+2],projectedVerts[i+3], projectedVerts[i+10],projectedVerts[i+11]])); } } } } // Copyright (c) 2008 David Wilhelm // MIT license: http://www.opensource.org/licenses/mit-license.php