package { import flash.display.Sprite; import flash.geom.*; import flash.events.Event; import flash.display.Graphics; public class FishSwim extends Sprite { public var colour:uint; private var vertices:Vector. = new Vector.(10); private var points:Vector.; public var velocity:Vector3D; private var commands:Vector.; public function FishSwim(col:uint,vel:Vector3D) { colour = col; velocity = vel; init(); } private function init():void { points = Vector.([ 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.([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.):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); } } } } // Copyright (c) 2008 David Wilhelm // MIT license: http://www.opensource.org/licenses/mit-license.php