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

3D Grid +FLV Load Test

Get Adobe Flash player
by hiro_rec 26 Feb 2009
    Embed
package
{
	import __AS3__.vec.Vector;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Matrix;
	import flash.geom.Vector3D;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.system.Security;
	
	
	[SWF (width="465", height="465", backgroundColor='#000000', frameRate=30)]
	
	public class GridMovie01 extends Sprite
	{
		private static const ROW_MAX:int = 15;
		private static const COL_MAX:int = 15;
		private static const VIDEO_URL:String = "http://start-rec.net/circleside/data/flash-sample/sample071029/logica-sample071029.flv";
		
		private var video:Video;
		private var connection:NetConnection;
		private var stream:NetStream;
		
		private var imageWidth:int, imageHeight:int;
		private var imageList:Vector.<MovieClip> = new Vector.<MovieClip>();
		private var imageBuffer:Vector.<BitmapData>;
		private var bufferIndex:int = 0;
		private var matrix:Matrix;
		
		private var containerWidth:int, containerHeight:int;
		private var container:Sprite;
		private var imageContainer0:Sprite;
		private var imageContainer1:Sprite;
		private var imageContainer2:Sprite;
		private var imageContainer3:Sprite;
		private var imageContainer4:Sprite;
		
		private var captBmd:BitmapData;
		
		private var imageBmp1:Bitmap;
		private var imageBmp2:Bitmap;
		private var imageBmp3:Bitmap;
		private var imageBmp4:Bitmap;

		
		public function GridMovie01()
		{
		         Security.loadPolicyFile("http://start-rec.net/crossdomain.xml"); 
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			imageWidth = stage.stageWidth / COL_MAX;
			imageHeight = stage.stageHeight / ROW_MAX;
			
			containerWidth = stage.stageWidth;
			containerHeight = stage.stageHeight;
			
			initContainer();
			initCapt();
			initVideo();
			initBitmap();
			
			addEventListener(Event.ENTER_FRAME, updateBuffer);
			addEventListener(Event.ENTER_FRAME, containerMove);
		}
		
		private function initContainer():void
		{
			container = new Sprite();
			imageContainer0 = new Sprite();
			imageContainer1 = new Sprite();
			imageContainer2 = new Sprite();
			imageContainer3 = new Sprite();
			imageContainer4 = new Sprite();
			
			imageContainer0.z = containerWidth / 2 * -1;
			
			imageContainer1.rotationY = -90;
			imageContainer1.x -= containerWidth / 2;
			imageContainer1.z = 0;
			
			imageContainer2.z = containerWidth / 2;
			
			imageContainer3.rotationY = 90;
			imageContainer3.x += containerWidth / 2;
			imageContainer3.z = 0;
			
			imageContainer4.rotationX = 90;
			//imageContainer4.x += containerWidth / 2;
			imageContainer4.z = 0;
			imageContainer4.y += containerHeight / 2;
			
			container.addChild(imageContainer4);
			container.addChild(imageContainer3);
			container.addChild(imageContainer2);
			container.addChild(imageContainer1);
			container.addChild(imageContainer0);
			
			container.x += containerWidth / 2;
			container.y += containerWidth / 2;
			container.z = 1000;
			container.rotationX = 45;
			
			addChild(container);
		}
		
		private function initVideo():void
		{
			connection = new NetConnection();
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			connection.connect(null);
		}
		
		private function initBitmap():void
		{
			imageBuffer = new Vector.<BitmapData>();
			
			var imageScaleX:Number = imageWidth / stage.stageWidth;
			var imageScaleY:Number = imageHeight / stage.stageHeight;
			
			matrix = new Matrix();
			//matrix.scale(imageScaleX, imageScaleY);
			
			var count:int = 0;
			
			for (var i:int = 0; i < ROW_MAX; i++)
			{
				for (var j:int = 0; j < COL_MAX; j++)
				{
					var bmd:BitmapData = new BitmapData(imageWidth, imageHeight, true, 0x00000000);
					var bmp:Bitmap = new Bitmap(bmd, "auto", true);
					var mc:MovieClip = new MovieClip();
					
					mc.index = count;
					mc.child = bmp;
					
					mc.x = imageWidth * j - imageWidth * COL_MAX / 2;
					mc.y = imageHeight * i - imageHeight * ROW_MAX / 2;
					
					mc.addEventListener(Event.ENTER_FRAME, render);
					mc.addChild(bmp);
					
					imageContainer0.addChild(mc);
					
					imageList.push(mc);
					
					imageBuffer.push(bmd.clone());
					
					count++;
				}
			}
			
			//imageList[0].visible = false;
		}
		
		private function initCapt():void
		{	
			captBmd = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
			
			imageBmp1 = new Bitmap(captBmd, "auto", true);
			imageBmp2 = new Bitmap(captBmd, "auto", true);
			imageBmp3 = new Bitmap(captBmd, "auto", true);
			imageBmp4 = new Bitmap(captBmd, "auto", true);
			
			imageBmp1.x -= imageBmp1.width / 2;
			imageBmp1.y -= imageBmp1.height / 2;
			
			imageBmp2.x -= imageBmp2.width / 2;
			imageBmp2.y -= imageBmp2.height / 2;
			
			imageBmp3.x -= imageBmp3.width / 2;
			imageBmp3.y -= imageBmp3.height / 2;
			
			imageBmp4.x -= imageBmp4.width / 2;
			imageBmp4.y -= imageBmp4.height / 2;
			
			imageContainer1.addChild(imageBmp1);
			imageContainer2.addChild(imageBmp2);
			imageContainer3.addChild(imageBmp3);
			imageContainer4.addChild(imageBmp4);
		}
		
		
		private function containerMove(event:Event):void
		{
			var dx:Number = stage.stageWidth / 2 - mouseX;
			var dy:Number = stage.stageHeight / 2 - mouseY;
			
			var vx:Number = (dx / 180 * 100 - container.rotationY) * 0.1;
			var vy:Number = (dy / 180 * 100 - container.rotationY) * 0.1;
			
			imageContainer0.transform.matrix3D.appendRotation(0.2, Vector3D.Y_AXIS);
			imageContainer1.transform.matrix3D.appendRotation(0.2, Vector3D.Y_AXIS);
			imageContainer2.transform.matrix3D.appendRotation(0.2, Vector3D.Y_AXIS);
			imageContainer3.transform.matrix3D.appendRotation(0.2, Vector3D.Y_AXIS);
			imageContainer4.transform.matrix3D.appendRotation(0.2, Vector3D.Y_AXIS);
			
			//trace(container.rotationX)
			//trace(container.z)
			
			container.rotationX += vy * 0.1;
			
			if (container.rotationX < -10)
				container.rotationX = -10;
			
			if (container.rotationX > 110)
				container.rotationX = 110;
			
			container.z += vx;
			
			if (container.z < -600)
				container.z = -600;
			
			var matrix:Matrix = new Matrix();
			matrix.translate(containerWidth /2, containerHeight / 2);
			captBmd.draw(imageContainer0, matrix);
			
			// Z sort
			var sortList:Array = [imageContainer0, imageContainer1, imageContainer2, imageContainer3, imageContainer4];
			var imageContainer:Sprite;
			sortList.sortOn("z", Array.NUMERIC | Array.DESCENDING);
			
			for(var i:int = 0; i < sortList.length; i++)
			{
				imageContainer = sortList[i] as Sprite;
				container.setChildIndex(imageContainer, i);
			}
			
		}
		
		private function updateBuffer(event:Event):void
		{
			if (bufferIndex >= imageBuffer.length)
				bufferIndex = 0;
			
			var bmd:BitmapData = imageBuffer[bufferIndex];
			bmd.draw(video, matrix, null, null, null, true);
			
			bufferIndex++;
		}
		
		
		private function render(event:Event):void
		{
			var mc:MovieClip = event.target as MovieClip;
			var bmp:Bitmap = mc.child;
			
			if (mc.index >= imageBuffer.length)
			{
				mc.index = 0;
			}
			
			var bmd:BitmapData = imageBuffer[mc.index];
			bmp.bitmapData = bmd;
			
			mc.index++;
		}
		
		
		private function netStatusHandler(event:NetStatusEvent):void
		{
			switch (event.info.code)
			{
				case "NetConnection.Connect.Success":
					connectStream();
				break;
				case "NetStream.Buffer.Flush":
					//stream.play(VIDEO_URL);
				case "NetStream.Play.Stop":
					stream.play(VIDEO_URL);
				break;
				case "NetStream.Play.StreamNotFound":
					trace("Unable to locate video: " + VIDEO_URL);
				break;
			}
        }
        
        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            trace(this + event);
        }
        
        private function asyncErrorHandler(event:AsyncErrorEvent):void
        {
             trace(this + event);
        }
        
        public function onMetaData(param:Object):void
        {
			
		}
		
		public function onXMPData(param:Object):void
		{
			
		}
		
        private function connectStream():void
        {
			stream = new NetStream(connection);
			stream.client = this;
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			
			video = new Video(imageWidth, imageHeight);
			video.attachNetStream(stream);
			stream.play(VIDEO_URL);
			//addChild(video);
        }

	}
}