강력한 pv3d로 상호작용 가능한 입방체 만들기

8697 단어
브라우저에서 플래시를 재생할 수 있도록 하려면
이전에oo를 이해하지 못할 때 썼는데 코드가 깨끗하지 않아요.
package
{
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Back;
	
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	
	/**
	 *
	 */		
	[SWF(backgroundColor = 0)]
	public class JdBox extends BasicView
	{
		private var box:Cube;
		private var plane : Plane;
		private var plane_line_top: Plane;
		private var plane_line_bottom : Plane;
		private var red : ColorMaterial;
		private var line:WireframeMaterial;
		private var redZu:CompositeMaterial;
		private var material:BitmapMaterial;
		
		private var planeLineList:Array=new Array();
		private var planeList : Array = new Array();
		
		public function JdBox()
		{
			super();
			stage.frameRate = 40;
			init();
			startRendering();
		}
		private function init():void
		{
			initBox();	
			setDefaultRotation();
			initPlanes();
			interactiveBox();
			
		
		}
		private function initBox():void
		{
			var rect:Shape = new Shape(); 
			rect.graphics.beginFill(0xFF0000,.5); 
			rect.graphics.lineStyle(2,0x000000,1);
			rect.graphics.drawRect(0,0,300,300);
			rect.graphics.endFill();
			
			var bmp:BitmapData = new BitmapData(300,300,true,0x0); 
			bmp.draw(rect);
			
			 material = new BitmapMaterial(bmp);
			 material.doubleSided = true;
			
//			line = new WireframeMaterial(0x000000,0.3,1); 		
//			
//			red = new ColorMaterial(0xFF0000,.3);
//		
//			redZu = new CompositeMaterial(); 
//			redZu.doubleSided = false;
//			redZu.addMaterial(red);
//			redZu.addMaterial(line);
			
			var materialsList : MaterialsList = new MaterialsList();
			materialsList.addMaterial(material,"front");
//			materialsList.addMaterial(material,"back");
//			materialsList.addMaterial(material,"left");
//			materialsList.addMaterial(material,"right");
//			materialsList.addMaterial(material,"top");
//			materialsList.addMaterial(material,"bottom");
			
			box = new Cube(materialsList,300,300,300);
			scene.addChild(box);
			
		}
		private function initPlanes():void
		{
			//----top----planeList[0]-----planeLineList[0]-----initPlaneLine[,,,,0,,,]
			initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,150,150,0,90,0,0);  
			
			//----bottom----planeList[1]-----planeLineList[1]-----initPlaneLine[,,,,1,,,]
			initSinglePlane( new Plane(material,300,300),0,-150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,-150,-150,1,90,0,0);  
			
			//----left----planeList[2]-----planeLineList[2]-----initPlaneLine[,,,,2,,,]
			initSinglePlane( new Plane(material,300,300),-150,0,0,0,0,90);
			initPlaneLine(new Plane(material,1,300),-150,0,150,2,0,90,0);  
			
			//----right----planeList[3]-----planeLineList[3]-----initPlaneLine[,,,,3,,,]
			initSinglePlane( new Plane(material,300,300),150,0,0,0,0,90);
			initPlaneLine(new Plane(material,1,300),150,0,-150,3,0,90,0);  
			
			
			//----front----planeList[4]-----planeLineList[4]-----initPlaneLine[,,,,4,,,]
			initSinglePlane( new Plane(material,300,300),0,150,0,0,0,0);
			initPlaneLine(new Plane(material,300,1),0,-150,-150,4,0,0,0);   
			
			interactivePlane();
		}
		private function initSinglePlane(tmpPlane : Plane,
											 x : int,y : int,z : int,
											 rotX : int,rotY : int,rotZ : int):void
		{
			tmpPlane.x = x;
			tmpPlane.y = y;
			tmpPlane.z = z;
			tmpPlane.localRotationX = rotX;
			tmpPlane.localRotationY = rotY;
			tmpPlane.localRotationZ = rotZ;
			planeList.push(tmpPlane);
		}
		private function initPlaneLine(planeLine : Plane , 
									     x : int , y : int , z : int,
										 planeindex:int,
										 rotX : int,rotY : int,rotZ : int):void
		{
			planeLine.x += x;
			planeLine.y += y;
			planeLine.z += z;
			planeLine.localRotationX = rotX;
			planeLine.localRotationY = rotY;
			planeLine.localRotationZ = rotZ;
			planeLine.addChild(planeList[planeindex]);
			planeLineList.push(planeLine);
			box.addChild(planeLine);
			
		}
		private function setDefaultRotation():void
		{
			box.localRotationX = 15;
			box.localRotationY = -30;
			box.localRotationZ = 0;
		}
		private function interactiveBox():void
		{
			viewport.interactive = true;
//			redZu.interactive = true;
			material.interactive = true;
			box.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,onOverHandler);
			box.addEventListener(InteractiveScene3DEvent.OBJECT_OUT,onOutHandler);
			box.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,onClickHandler);

		}
		private function interactivePlane():void
		{
			for(var i:int=0; i < planeList.length;i++){
			planeList[i].addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,planeClickHandler);
			}
		}
		/**
		 *    plane,             ,               plane
		 */
		private function planeClickHandler(event : InteractiveScene3DEvent):void
		{
			rotateBox(event.target as Plane);
			rotateCrtPlane(event.target as Plane);	
			
		}
		private function rotateBox(tmpPlane : Plane):void
		{
			var tmpIndex:int=planeList.indexOf(tmpPlane,0);
			if(tmpIndex == 3){
				TweenLite.to(box,.2,{localRotationY:box.localRotationY-90,
									 onComplete:onFinishTween1});
				
//				TweenLite.to(clip_mc, 5, {alpha:0.5, x:120, ease:Back.easeOut, delay:2, 
//					onComplete: onFinishTween, onCompleteParams:[5, clip_mc]});
			}
		}
		private function onFinishTween1():void
		{
			TweenLite.to(box,.2,{localRotationZ:box.localRotationZ-90,
								onComplete:onFinishTween2});
		}
		private function onFinishTween2():void
		{
			trace("finish 2");
			TweenLite.to(box,.2,{localRotationX:box.localRotationX-90});
		}
		private function rotateCrtPlane(tmpPlane : Plane):void
		{
			var tmpIndex:int=planeList.indexOf(tmpPlane,0);
			var tmpPlaneLine : Plane = planeLineList[tmpIndex];
			switch(tmpIndex){
				case 0:   //top	 ,the same to bottom					
				case 1:  //bottom
					switch(tmpPlaneLine.localRotationX){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
							break;
					}
					break;
				case 2: //left
				case 3: //right
					switch(tmpPlaneLine.localRotationY){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationY:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationY:0,ease:Back.easeOut});
							break;
					}
					break;
				case 4:  //front
					switch(tmpPlaneLine.localRotationX){
						case 0:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:90,ease:Back.easeOut});
							break;
						case 90:
							TweenLite.to(tmpPlaneLine,2,{localRotationX:0,ease:Back.easeOut});
							break;
					}
					break;
			}
		}
		private function onOverHandler(event : InteractiveScene3DEvent):void
		{
			trace("ok");
			viewport.buttonMode = true;
		//	box.materials.getMaterialByName("back").fillAlpha = .8;
			
			
		}
		private function onOutHandler(event : InteractiveScene3DEvent):void
		{
			trace("no");
			viewport.buttonMode = false;
		//	box.materials.getMaterialByName("back").fillAlpha = .3;
			
		}
		/**
		 *        ,            plane     
		 */
		private function onClickHandler(event : InteractiveScene3DEvent):void
		{
			randomRotateBox();		
//			rotationBox();
			
		}
		private function randomRotateBox():void
		{
			var tmpNum : Number = Math.random();
			if(tmpNum > .6){
				TweenLite.to(box,2,{localRotationX:box.localRotationX+50,ease:Back.easeOut});
			}else if(tmpNum < .3){
				TweenLite.to(box,2,{localRotationY:box.localRotationY+50,ease:Back.easeOut});
			}else{
				TweenLite.to(box,2,{localRotationZ:box.localRotationZ+50,ease:Back.easeOut});
			}
		}
		private function rotationBox():void
		{
			TweenLite.to(box,2,{rotationX :box.rotationX +50,ease:Back.easeOut});
//			box.yaw(30);
		}
//		override protected function onRenderTick(e:Event=null):void
//		{
//			//box.localRotationX++;
//			//box.localRotationY++;
//			//box.localRotationZ++;
//			super.onRenderTick();
//		}
		
	}
}

전재 대상:https://www.cnblogs.com/JD85/archive/2010/05/09/1730923.html

좋은 웹페이지 즐겨찾기