⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interactivescenemanager.as

📁 Flex3实现的掼蛋游戏
💻 AS
📖 第 1 页 / 共 2 页
字号:

		   buttonMode = true;
		   faceLevelMode = true;
		   mouseInteractionMode = false;
		}

		/**
		* Adds a DisplayObject3D or Face3D object to the ISM
		* @param	container3d
		* @return
		*/
		public function addInteractiveObject(container3d:Object):void
		{
			if(faceDictionary[container3d] == null) 
			{
				var icd:InteractiveContainerData = faceDictionary[container3d] = new InteractiveContainerData(container3d);
				
				// for reverse lookup when you have the sprite container
				containerDictionary[icd.container] = container3d;
				
				// add mouse events to be captured and passed along
				var icdContainer:InteractiveSprite = icd.container;
				icdContainer.addEventListener(MouseEvent.MOUSE_DOWN, handleMousePress);
				icdContainer.addEventListener(MouseEvent.MOUSE_UP, handleMouseRelease);
				icdContainer.addEventListener(MouseEvent.CLICK, handleMouseClick);
				icdContainer.addEventListener(MouseEvent.MOUSE_OVER, handleMouseOver);
				icdContainer.addEventListener(MouseEvent.MOUSE_OUT, handleMouseOut);
				icdContainer.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
				
				icdContainer.buttonMode = buttonMode;
				if( !SHOW_DRAWN_FACES && !DisplayObject3D.faceLevelMode ) icdContainer.blendMode = BlendMode.ERASE;
				
				// need to let virtualMouse know what to ignore
				virtualMouse.ignore(icdContainer);
				
				// let others know we've added a container
				dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_ADDED, null, icdContainer));
				
				if(debug) log.debug("addDisplayObject id", container3d.id, container3d.name, DEFAULT_SPRITE_ALPHA);
			}
		}
		
		/**
		* drawFace is called from each of the interactive materials in the render loop.  It either receives DisplayObject3D or a Face3D object reference.  It then creates a container
		 * if one doesn't exist.  Then, it draws the face into the designated container which is an InteractiveSprite.
		* 
		* @param	container3d
		* @param	face3d
		* @param	x0
		* @param	x1
		* @param	x2
		* @param	y0
		* @param	y1
		* @param	y2
		* @return
		*/
		public function drawFace(container3d:DisplayObject3D, face3d:Face3D, x0:Number, x1:Number, x2:Number, y0:Number, y1:Number, y2:Number ):void
		{
			// if we're face level on this DO3D, then we switch to the face3D object
			var container:Object = container3d;
			if(faceLevelMode || DisplayObject3D.faceLevelMode) container = face3d;
			
			// add to the dictionary if not added already
			if(faceDictionary[container] == null) addInteractiveObject(container);

			if( allowDraw && !DisplayObject3D.faceLevelMode )
			{
				var drawingContainer:InteractiveContainerData = faceDictionary[container];
				var iContainer:InteractiveSprite = drawingContainer.container;
				var graphics:Graphics = iContainer.graphics;
				
				
				iContainer.x0 = x0;
				iContainer.x1 = x1;
				iContainer.x2 = x2;
				iContainer.y0 = y0;
				iContainer.y1 = y1;
				iContainer.y2 = y2;
				
				graphics.beginFill(drawingContainer.color, drawingContainer.fillAlpha);
				if( drawingContainer.lineColor != -1 && SHOW_DRAWN_FACES ) graphics.lineStyle(drawingContainer.lineSize, drawingContainer.lineColor, drawingContainer.lineAlpha);
				graphics.moveTo( x0, y0 );
				graphics.lineTo( x1, y1 );
				graphics.lineTo( x2, y2 );
				graphics.endFill();
				drawingContainer.isDrawn = true;
			}
		}
		
		/*
		public function getSprite(container3d:DisplayObject3D):InteractiveSprite
		{
			return InteractiveContainerData(faceDictionary[container3d]).container;
		}
		
		public function getDisplayObject3D(sprite:InteractiveSprite):DisplayObject3D
		{
			return DisplayObject3D(containerDictionary[sprite]);
		}
		*/
		
		/**
		 * When called, aligns the ISM's container with the scene's container to make sure faces drawn in the ISM are aligned with the scene perfectly
		 * 
		 */		
		public function resizeStage():void
		{
			container.x = scene.container.x;
			container.y = scene.container.y;
		}
		
		/**
		 * When called, all faces are cleared and the isDrawn flag is reset on the InteractiveContainerData objects to ready them for the next loop.
		 * 
		 * This is called via InteractiveScene3D before the render.
		 * 
		 */		
		public function resetFaces():void
		{			
			// clear all triangles/faces that have been drawn
			for each( var item:InteractiveContainerData in faceDictionary)
			{
				item.container.graphics.clear();
				item.sort = item.isDrawn;
				item.isDrawn = false;
			}
			
			// make sure the sprite is aligned with the scene's canvas
			resizeStage();
		}
		
		/**
		 * After the render loop is completed, InteractiveScene3D calls this method to sort the interactive scene objects.  If nothing was drawn into a container, it's completely 
		 * ignored at this level as well.
		 * 
		 */		
		public function sortObjects():void
		{
			// called from the scene after the render loop is completed
			var sort:Array = [];
			
			for each( var item:InteractiveContainerData in faceDictionary)
			{
				if(!item.sort) continue;
				var distance:Number = item.face3d == null ? item.screenZ : item.face3d.face3DInstance.screenZ;
				sort.push({container:item.container, distance:distance});
			}
			
			sort.sortOn("distance", Array.DESCENDING | Array.NUMERIC);
			
			for(var i:uint=0;i<sort.length;i++) container.addChild(sort[i].container);
			
			// after the render loop is complete, and we've sorted, we reset the allowDraw flag
			if( mouseInteractionMode ) allowDraw = false;
		}
		
		/**
		 * @private
		 * @param e
		 * 
		 */		
		protected function handleAddedToStage(e:Event):void
		{
			container.stage.addEventListener (Event.RESIZE, handleResize);
			container.stage.addEventListener(MouseEvent.MOUSE_UP, handleReleaseOutside);
			
			virtualMouse.stage = container.stage;
		}
		
		/**
		 * Handles the MOUSE_DOWN event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMousePress(e:MouseEvent):void
		{
			MOUSE_IS_DOWN = true;
			if( virtualMouse ) virtualMouse.press();
			dispatchObjectEvent(InteractiveScene3DEvent.OBJECT_PRESS, Sprite(e.currentTarget));
		}
		/**
		 * Handles the MOUSE_UP event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMouseRelease(e:MouseEvent):void
		{
			MOUSE_IS_DOWN = false;
			if( virtualMouse ) virtualMouse.release();
			dispatchObjectEvent(InteractiveScene3DEvent.OBJECT_RELEASE, Sprite(e.currentTarget));
		}
		/**
		 * Handles the MOUSE_CLICK event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMouseClick(e:MouseEvent):void
		{
			dispatchObjectEvent(InteractiveScene3DEvent.OBJECT_CLICK, Sprite(e.currentTarget));
		}
		/**
		 * Handles the MOUSE_OVER event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMouseOver(e:MouseEvent):void
		{
			var eventType:String
			eventType = !evaluateClick || !mouseInteractionMode ? InteractiveScene3DEvent.OBJECT_OVER : InteractiveScene3DEvent.OBJECT_CLICK;
			evaluateClick = false;
			
			if( virtualMouse && eventType == InteractiveScene3DEvent.OBJECT_CLICK ) virtualMouse.click()
			dispatchObjectEvent(eventType, Sprite(e.currentTarget));
		}
		/**
		 * Handles the MOUSE_OUT event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMouseOut(e:MouseEvent):void
		{
			if( VirtualMouse && ( faceLevelMode || DisplayObject3D.faceLevelMode ))
			{
				try
				{
					var face3d:Face3D = containerDictionary[e.currentTarget];
					var p:Object = InteractiveUtils.getMapCoordAtPoint(face3d, container.mouseX, container.mouseY);
					
					var mat:InteractiveMovieMaterial = InteractiveMovieMaterial(face3d.face3DInstance.instance.material);
					var rect:Rectangle = new Rectangle(0, 0, mat.movie.width, mat.movie.height);
					var contains:Boolean = rect.contains(p.x, p.y);
					
					if (!contains) virtualMouse.exitContainer();
				}catch(err:Error)
				{
					log.error("material type is not Interactive.  If you're using a Collada object, you may have to reassign the material to the object after the collada scene is loaded", err.message);
				}
			}
			
			dispatchObjectEvent(InteractiveScene3DEvent.OBJECT_OUT, Sprite(e.currentTarget));
		}
		/**
		 * Handles the MOUSE_MOVE event on an InteractiveSprite container
		 * @param e
		 * 
		 */		
		protected function handleMouseMove(e:MouseEvent):void
		{	
			var point:Object;
			if( VirtualMouse && ( faceLevelMode || DisplayObject3D.faceLevelMode ))
			{
				// need the face3d for the coordinate conversion
				var face3d:Face3D = containerDictionary[e.currentTarget];
				
				// get 2D coordinates
				point = InteractiveUtils.getMapCoordAtPoint(face3d, container.mouseX, container.mouseY);
				
				//log.debug("material type", ObjectTools.getImmediateClassPath(face3d.face3DInstance.instance.material), face3d.face3DInstance.instance.material is InteractiveMovieMaterial);
				try
				{
					// locate the material's movie
					var mat:MovieMaterial = face3d.face3DInstance.instance.material as MovieMaterial;

					// set the location where the calcs should be performed
					virtualMouse.container = mat.movie as Sprite;
						
					// update virtual mouse so it can test
					if( virtualMouse.container ) virtualMouse.setLocation(point.x, point.y);
				}catch(err:Error)
				{
					log.error("material type is not Inter active.  If you're using a Collada object, you may have to reassign the material to the object after the collada scene is loaded", err.message);
				}
			}
			
			dispatchObjectEvent(InteractiveScene3DEvent.OBJECT_MOVE, Sprite(e.currentTarget));
			
			if( Mouse3D.enabled && ( faceLevelMode || DisplayObject3D.faceLevelMode ) ) 
			{
				mouse3D.updatePosition(Face3D(containerDictionary[e.currentTarget]), e.currentTarget as Sprite);
			}
		}
		/**
		 * Handles mouse clicks on the stage.  If so, we release a releaseOutside event
		 * @param e
		 * 
		 */		
		protected function handleReleaseOutside(e:MouseEvent):void
		{	
			if(debug) log.debug("releaseOutside");
			dispatchEvent(new InteractiveScene3DEvent(InteractiveScene3DEvent.OBJECT_RELEASE_OUTSIDE));
			MOUSE_IS_DOWN = false;
			evaluateClick = true
			allowDraw = true;
		}
		/**
		 * When ISM is in mouseInteractionMode, we capture mouse move events from the stage to trigger a draw
		 * @param e
		 * 
		 */		
		protected function handleStageMouseMove(e:MouseEvent):void
		{
			allowDraw = true;
		}
		
		/**
		 * @private
		 * @param event
		 * @param currentTarget
		 * 
		 */		
		protected function dispatchObjectEvent(event:String, currentTarget:Sprite):void
		{
			if(debug) log.debug(event, DisplayObject3D(containerDictionary[currentTarget]).name);
			
			if(containerDictionary[currentTarget] is DisplayObject3D)
			{
				containerDictionary[currentTarget].dispatchEvent(new InteractiveScene3DEvent(event, containerDictionary[currentTarget], InteractiveSprite(currentTarget)));
				dispatchEvent(new InteractiveScene3DEvent(event, containerDictionary[currentTarget], InteractiveSprite(currentTarget), null, null));
			}else if(containerDictionary[currentTarget] is Face3D)
			{
				var face3d:Face3D = containerDictionary[currentTarget];
				var face3dContainer:InteractiveContainerData = faceDictionary[face3d];
				dispatchEvent(new InteractiveScene3DEvent(event, null, InteractiveSprite(currentTarget), face3d, face3dContainer));
			}
		}
		
		/**
		 * @private
		 * @param e
		 * 
		 */		
		protected function handleResize(e:Event):void
		{
			resizeStage();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -