egret作为一个HTML5的游戏引擎居然连基本的摄像机都没有,让我这个做游戏开发的十分不习惯,于是自己动手去做了一个类似于摄像机的一个类
原理大概是这样的:创建一个容器 利用这个容器放大缩小来模拟摄像机的zoom 可以动态修改锚点改变摄像机的观察位置 移动整个场景来模拟摄像机的移动 废话不多说 直接上代码:
module com.controller{
//镜头管理类 用来移动 拉伸镜头
export class CameraManager extends egret.DisplayObjectContainer{
//目标容器
private targetObject:any;
//单例
static _instance:CameraManager;
//摄像机的移动速度
private speed:number = 10;
//上一个容器的X位置
private lastTargetX:number = 0;
//上一个容器的Y位置
private lastTargetY:number = 0;
/**
* @status
* STAY 停止
* MOVEDOWN 向下移动
* MOVEUP 向上移动
* MOVELEFT 向左移动
* MOVERIGHT 向右移动
* */
private status:string = "STAY";
/**
* 总共需要的移动距离x
* */
private distanceX:number = 0;
/**
* 总共需要的移动距离y
* */
private distanceY:number = 0;
/**
*移动距离x
**/
private moveX:number = 0;
/**
* 移动距离y
**/
private moveY:number = 0;
//构造函数
constructor(){
super();
this.addEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this)
}
setAt(target){
this.targetObject = target;
}
//聚焦到某一个物体的锚点上
lookAt(target:any){
//this.lastTargetX = target.x;
//this.lastTargetY = target.y;
AnchorUtil.setAnchorX(this.targetObject, target.x/this.targetObject.width);
AnchorUtil.setAnchorY(this.targetObject, target.y/this.targetObject.height);
this.targetObject.x = this.targetObject.x+(target.x-this.lastTargetX)*com.model.DataCenter.instance.configVO.whRate;
this.targetObject.y = this.targetObject.y+(target.y-this.lastTargetY)*com.model.DataCenter.instance.configVO.whRate;
this.lastTargetX = target.x;
this.lastTargetY = target.y;
}
//放大或者缩小
zoom(pix){
egret.Tween.get(this.targetObject).to({scaleX:pix,scaleY:pix},300,egret.Ease.sineIn).to({scaleX:1,scaleY:1},300,egret.Ease.sineIn);
}
//向某个方向一定距离
moveTo(distance:number,status:string){
this.status = status;
switch (status){
case "MOVEDOWN":
this.distanceY = distance;
break;
case "MOVELEFT":
this.distanceX = distance;
break;
case "MOVEUP":
this.distanceY = distance;
break;
case "MOVERIGHT":
this.distanceX = distance;
break;
}
}
//tween到某个位置
private onEnterFrame(e:egret.TouchEvent){
switch (this.status){
case "STAY":
this.moveY = 0;
this.distanceY = 0;
this.moveX = 0;
this.distanceX = 0;
break;
case "MOVEUP":
this.targetObject.y += this.speed;
this.moveY+=this.speed;
if(this.moveY>=this.distanceY){
this.status = "STAY";
}
this.targetObject.x += 0;
break;
case "MOVEDOWN":
this.targetObject.y -= this.speed;
this.moveY+=this.speed;
if(this.moveY>=this.distanceY){
this.status = "STAY";
}
this.targetObject.x += 0;
break;
case "MOVERIGHT":
this.targetObject.x -= this.speed;
this.moveX+=this.speed;
if(this.moveX>=this.distanceX){
this.status = "STAY";
}
this.targetObject.y += 0;
break;
case "MOVELEFT":
this.targetObject.x += this.speed;
this.targetObject.y += 0;
this.moveX+=this.speed;
if(this.moveX>=this.distanceX){
this.status = "STAY";
}
break;
}
}
static get instance():CameraManager{
if(this._instance==null){
this._instance = new CameraManager()
}
return this._instance;
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。