对白鹭引擎的了解也有一个的时间了,期间也尝试过几个小的游戏练手,但是好像官方并没有对场景切换做封装,所以如下来尝试着动手模拟一出。
新建一个egret的游戏项目
1.具体的目录结构就不在一一描述,在src文件夹下面会生成有两个文件,一个是LoadingUI.ts加载类,另一个是Main.ts项目的入口。
接下来删除掉Main.ts文件里面的createGameScene这个方法里面的所有内容,然后在这里面做一些业务逻辑。为了项目和代码的美观,这里只作为一个小的入口,将业务逻辑独立在一个新的文件里面。
代码如下:
1 2 3 4 5 6
| Main.ts: private createGameScene() { var viewManager: ViewManager = new ViewManager(); this.addChild(viewManager); viewManager.start(); }
|
2.在src里新建一个文件夹,在这个文件夹下面存放一个文件用来作为视图控制管理的逻辑代码,命名为ViewManager.ts
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class ViewManager extends egret.Sprite{ public constructor(){ super(); this.init(); }
private gameStartPanel: GameStartPanel; private gameEndPanel: GameEndPanel; private gamePlayingPanel: GamePlayingPanel; public init(){ this.gameStartPanel = new GameStartPanel(); this.gameEndPanel = new GameEndPanel(); this.gamePlayingPanel = new GamePlayingPanel(); } public start(){ this.addChild(this.gameStartPanel); this.gameStartPanel.start(); this.gameStartPanel.addEventListener(GameStartPanel.GAME_START, this.gamePlaying,this); } private gamePlaying() { this.gameStartPanel.end(); this.removeChild(this.gameStartPanel); this.gameStartPanel.removeEventListener(GameStartPanel.GAME_START,this.gamePlaying,this); this.addChild(this.gamePlayingPanel); this.gamePlayingPanel.start(); this.gamePlayingPanel.addEventListener(GamePlayingPanel.CHANGEPANEL,this.gameEnd,this); } private gameEnd() { this.gamePlayingPanel.end(); this.removeChild(this.gamePlayingPanel); this.gamePlayingPanel.removeEventListener(GamePlayingPanel.CHANGEPANEL,this.gameEnd,this);
this.addChild(this.gameEndPanel); this.gameEndPanel.start(); this.gameEndPanel.addEventListener(GameEndPanel.GAME_END,this.gameStart,this); } private gameStart(){ this.gameEndPanel.end(); this.removeChild(this.gameEndPanel); this.gameEndPanel.removeEventListener(GameEndPanel.GAME_END,this.gameStart,this);
this.addChild(this.gameStartPanel); this.gameStartPanel.start(); this.gameStartPanel.addEventListener(GameStartPanel.GAME_START,this.gamePlaying,this) } }
|
- 接下来处理每个面板里面的业务,每个文件里面有备注,具体看备注
GameStartPanel.ts
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| class GameStartPanel extends egret.Sprite { public static GAME_START: string = "gameStart"; private bg: egret.Bitmap; private startBtn: egret.TextField; public constructor() { super(); this.init(); } public start() { this.startBtn.touchEnabled = true; this.startBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchTab,this); } private init() { this.bg = new egret.Bitmap(RES.getRes('gameStartBgImage')); this.addChild(this.bg); this.startBtn = new egret.TextField(); this.startBtn.text = '开始游戏'; this.addChild(this.startBtn); this.startBtn.x = (480 - this.startBtn.width) * 0.5; this.startBtn.y = 400; }
private onTouchTab(e: egret.TouchEvent) { this.dispatchEventWith(GameStartPanel.GAME_START); } public end() { this.startBtn.touchEnabled = false; if(this.startBtn.hasEventListener(egret.TouchEvent.TOUCH_TAP)) this.startBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchTab,this); } }
|
GamePlayingPanel.ts
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| class GamePlayingPanel extends egret.Sprite { public static CHANGEPANEL: string = "changepanel"; private bg: egret.Bitmap; private timeTitle: egret.TextField; private timer: egret.Timer; private timeNumbers: number = 20; public constructor() { super(); this.init(); } public start() { this.timeNumbers = 20; this.timer.start(); this.timer.addEventListener(egret.TimerEvent.TIMER,this.onTimer,this); this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.onTimerComplete,this); } private init() { this.bg = new egret.Bitmap(RES.getRes('gamePlayingBgImage')); this.addChild(this.bg);
this.timeTitle = new egret.TextField(); this.timeTitle.text = "剩余时间:" + this.timeNumbers + " 秒"; this.timeTitle.x = (480 - this.timeTitle.width) * 0.5; this.timeTitle.y = 400; this.addChild(this.timeTitle); this.timer = new egret.Timer(1000,this.timeNumbers); }
private onTimer(e:egret.TimerEvent){ this.timeNumbers -= 1; this.timeTitle.text = "剩余时间:" + this.timeNumbers + " 秒"; } private onTimerComplete(e: egret.TouchEvent) { this.dispatchEventWith(GamePlayingPanel.CHANGEPANEL); } public end() { if(this.timer.hasEventListener(egret.TimerEvent.TIMER)) this.timer.removeEventListener(egret.TimerEvent.TIMER,this.onTimer,this); if(this.timer.hasEventListener(egret.TimerEvent.TIMER_COMPLETE)) this.timer.removeEventListener(egret.TimerEvent.TIMER_COMPLETE,this.onTimerComplete,this); this.timer.stop(); this.timer.reset(); } }
|
GameEndPanel.ts
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class GameEndPanel extends egret.Sprite { public static GAME_END: string = "gameEnd"; private bg: egret.Bitmap; private restartBtn: egret.TextField; public constructor() { super(); this.init(); } public start() { this.restartBtn.touchEnabled = true; this.restartBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchTab,this); } private init() { this.bg = new egret.Bitmap(RES.getRes('gameEndBgImage')); this.addChild(this.bg);
this.restartBtn = new egret.TextField(); this.restartBtn.text = '重新开始游戏'; this.addChild(this.restartBtn); this.restartBtn.x = (480 - this.restartBtn.width) * 0.5; this.restartBtn.y = 400; }
private onTouchTab(e: egret.TouchEvent) { this.dispatchEventWith(GameEndPanel.GAME_END); } public end() { this.restartBtn.touchEnabled = false; if(this.restartBtn.hasEventListener(egret.TouchEvent.TOUCH_TAP)) this.restartBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchTab,this); } }
|