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; } } }