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

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/bc3h
 */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.*;
	import flash.display.DisplayObjectContainer;
	import flash.geom.*;
	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>();
		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();
				});
		}

		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.*;
	import flash.events.Event;
	import flash.display.Graphics;
	

class FishSwim extends Sprite {
	public var colour:uint;
	private var	vertices:Vector.<Number> = new Vector.<Number>(10);
	private var points:Vector.<Point>;
	public var velocity:Vector3D;
	private var commands:Vector.<int>;

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

	}
	private function init():void {
			points = Vector.<Point>([
		new Point(-100,-40),//top left corner (top of tail)
		new Point(0,80),//bottom curve target - fish's belly
		new Point(100,0),//head
		new Point(0,-80),//top curve target (fish's back)
		new Point(-100,40)//bottom left corner (bottom of tail)
	 ]);
		commands = Vector.<int>([1,3,3]);
	}
	public function render(/*e:Event*/):void {
		//orient the fish in the direction of its motion
		transform.matrix3D.pointAt(new Vector3D(x + velocity.x*100,y + velocity.y*100, z + velocity.z*100), new Vector3D(1,0,0), new Vector3D(0,-1,0));

		//we must convert the array of points to a vertices vector
		points.forEach(function(i:Point,n:int, a:Vector.<Point>):void {
			var gp:Point = localToGlobal(i);
			vertices[n*2] = gp.x;
			vertices[n*2+1] = gp.y;
		});
		//draw fish using globalized points on the root
		var g:Graphics = (root as Sprite).graphics;
		if(z > -600){//cull so they don't obscure the screen
			g.beginFill(colour);
			g.drawPath(commands,vertices);
		}
	}

}