In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

swimming fishes - project vectors

Get Adobe Flash player
by bigfish 18 Aug 2009
/**
 * Copyright bigfish ( http://wonderfl.net/user/bigfish )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7QsF
 */

// forked from bigfish's swimming fishes
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.*;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;

	[SWF(backgroundColor="0x000000", width="800", height="600", frameRate="30")]	
	public class FishesSwim extends Sprite {

		public var fishes:Vector.<FishSwim> = new Vector.<FishSwim>();
		private var fishvertices:Vector.<Number> = Vector.<Number>([ -100,-40,0, 0,80,0, 100,0,0, 0,-80,0, -100,40,0 ]);
		private var commands:Vector.<int> = Vector.<int>([1,3,3]);
		private var perspective:PerspectiveProjection = new PerspectiveProjection();
		private var	vertices:Vector.<Number> = new Vector.<Number>();
		private var uvts:Vector.<Number> = new Vector.<Number>();
		

		public function FishesSwim() {
			init();
		}
		private function init():void {
			for(var i:int = 0; i < 50; i++){
				var fish:FishSwim = new FishSwim(Math.random()*0xFFFFFF,
					new Vector3D(Math.random()*2-1,Math.random()*2-1,Math.random()*2-1) /*velocity*/);
				fish.x= Math.random()*stage.stageWidth;
				fish.y= Math.random()*stage.stageHeight;
				fish.z= Math.random()*50;
			    addChild(fish);
				fishes.push(fish);
			}
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		private function onEnterFrame(event:Event):void {
			(root as Sprite).graphics.clear();
			zSort(fishes);
			fishes.forEach(function(fish:FishSwim,i:int,a:Vector.<FishSwim>):void {
					//move
					fish.x += fish.velocity.x;
					fish.y += fish.velocity.y;
					fish.z += fish.velocity.z;
					//fish.render();
					fish.transform.matrix3D.pointAt(new Vector3D(fish.x + fish.velocity.x*100,fish.y + fish.velocity.y*100, fish.z + fish.velocity.z*100), new Vector3D(1,0,0), new Vector3D(0,-1,0));
					var m:Matrix3D = perspective.toMatrix3D();
					Utils3D.projectVectors(fish.transform.matrix3D,fishvertices,vertices,uvts);
					graphics.beginFill(fish.colour);
					graphics.drawPath(commands,vertices);

				});
		}

		private function zSort(sprites:Vector.<FishSwim>):void {
			sprites = sprites.sort(function(a:FishSwim, b:FishSwim):Number { return b.z-a.z; });
		}
	}
}



import flash.display.Sprite;
import flash.geom.*;

class FishSwim extends Sprite {
	public var colour:uint;
	public var velocity:Vector3D;

	public function FishSwim(col:uint,vel:Vector3D) {
		colour = col;
		velocity = vel;
	}

}