关闭

AS3 自己写的一个2D游戏摄像机类,JCamera

标签: 游戏functiondictionaryimportstringclass
1760人阅读 评论(0) 收藏 举报
分类:

在参考了别人的效果之后。


经整理了一下思路,得出了2D游戏摄像机的思路。


简单的写了一个,功能可以扩展:

package view
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	/**
	 * 摄像头类
	 * @author Jave.Lin
	 */	
	public class JCamera extends Sprite
	{
		private var _camera_Width:Number=0;
		private var _camera_Height:Number=0;
		
		private var _layerContainerDic:Dictionary=new Dictionary();
		
		public function get camera_Width():Number{return _camera_Width;}
		public function get camera_Height():Number{return _camera_Height;}
		//目标点
		public var targetPosition:Point;
		//上次点
		public var lastPosition:Point;
		//当前要偏移的点
		public var offsetPosition:Point;
		
		public function JCamera($targetPosition:Point,$width:Number=1200,$height:Number=700)
		{
			super();
			setSize($width,$height);
			targetPosition=$targetPosition;
			offsetPosition=new Point();
			lastPosition=new Point();
		}
		
		public function addLayerContainer(containerName:String,dc:DisplayObjectContainer):void
		{
			if(_layerContainerDic.hasOwnProperty(dc.name))return;
			_layerContainerDic[dc.name]=new CameraContainer(dc.x,dc.y,dc,containerName);
		}
		
		public function setSize($width:Number,$height:Number):void
		{
			_camera_Width=$width;
			_camera_Height=$height;
			refreshSize();
		}
		
		public function refreshSize():void
		{
			this.graphics.beginFill(0,0);
			
			this.graphics.drawRect(
				-camera_Width/2,-camera_Height/2,
				camera_Width,camera_Height);
			
			this.graphics.endFill();
		}
		
		public function update():void
		{
			offsetPosition.x=targetPosition.x-lastPosition.x;
			offsetPosition.y=targetPosition.y-lastPosition.y;
			
			for each (var cc:CameraContainer in _layerContainerDic) 
			{
				cc.container.x+=-offsetPosition.x;
				cc.container.y+=-offsetPosition.y;
			}
			
			lastPosition.x=targetPosition.x;
			lastPosition.y=targetPosition.y;
		}
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论