这次给大家带来的是通过Egret实现密室逃生小游戏的教程。该游戏包括人物状态机、MVC设计模式和单例模式,该游戏在1.5s内通过玩家点击操作寻找安全点,方可进入下一关,关卡无限,分数无限。下面是具体的模块介绍和代码实现。

该游戏主要内容包括

  • 开始游戏场景

  • 游戏场景

  • 游戏结束结算场景

  • 全局常量类

  • 人物状态机类

游戏源码素材下载:https://github.com/shenysun/RoomRun

在所有舞台搭建之前先写一个全局的静态方法类,取名为GameConst。这个类里面的方法和常量可以供全局使用,例如舞台宽高、通过名字获取位图、通过名字获取纹理集精灵等等。这个类可以大大减少后期的代码量,降低整体的耦合度。

 

  1. /**常用常量类 */
  2. class GameConst {
  3. /**舞台宽度 */
  4. public static StageW:number;
  5. /**舞台高度 */
  6. public static StageH:number;
  7. /**根据名字创建位图 */
  8. public static CreateBitmapByName(name:string):egret.Bitmap {
  9. let texture:egret.Texture = RES.getRes(name);
  10. let bitmap:egret.Bitmap = new egret.Bitmap(texture);
  11. return bitmap;
  12. }
  13. /**
  14. * 根据name关键字创建一个Bitmap对象。此name 是根据TexturePacker 组合成的一张位图
  15. */
  16. public static createBitmapFromSheet(name:string, sheetName:string):egret.Bitmap {
  17. let texture:egret.Texture = RES.getRes(`${sheetName}_json.${name}`);
  18. let result:egret.Bitmap = new egret.Bitmap(texture);
  19. return result;
  20. }
  21. public static getTextureFromSheet(name:string, sheetName:string):egret.Texture {
  22. let result:egret.Texture = RES.getRes(`${sheetName}_json.${name}`);
  23. return result;
  24. }
  25. /**移除子类方法 */
  26. public static removeChild(child:egret.DisplayObject) {
  27. if(child && child.parent) {
  28. if((<any>child.parent).removeElement) {
  29. (<any>child.parent).removeElement(<any>(child));
  30. }
  31. else {
  32. child.parent.removeChild(child);
  33. }
  34. }
  35. }
  36. }

 

如果游戏中设置图片锚点较多也可以在这个类里面加一个设置锚点的方法,传入对象,横轴锚点和纵轴锚点坐标三个参数。

 

开始页面比较简洁,有一个LOGO和两个按钮分别是开始游戏,更多游戏。

  1. /**游戏开始场景 */
  2. class StartGameLayer extends egret.Sprite {
  3. /**开始按钮 */
  4. private startBtn:MyButton;
  5. /**更多按钮 */
  6. private moreBtn:MyButton;
  7. /**LOGO */
  8. private titleImage:egret.Bitmap;
  9. public constructor() {
  10. super();
  11. this.init();
  12. }
  13. private init():void {
  14. /**添加游戏LOGO */
  15. this.titleImage = GameConst.createBitmapFromSheet("logo_mishitaosheng", "ui");
  16. this.titleImage.x = 51;
  17. this.titleImage.y = 161;
  18. this.addChild(this.titleImage);
  19. //开始按钮设置
  20. this.startBtn = new MyButton("btn_y", "btn_kaishi");
  21. this.addChild(this.startBtn);
  22. this.startBtn.x = (GameConst.StageW - this.startBtn.width) / 2;
  23. this.startBtn.y = GameConst.StageH / 2 - 75;
  24. this.startBtn.setClick(this.onStartGameClick);
  25. //更多按钮设置
  26. this.moreBtn = new MyButton("btn_b", "btn_gengduo");
  27. this.moreBtn.x = (GameConst.StageW - this.startBtn.width) / 2;
  28. this.moreBtn.y =GameConst.StageH / 2 + 75;
  29. this.addChild(this.moreBtn);
  30. this.moreBtn.setClick(this.onMoreBtnClick);
  31. //文本
  32. let tex:egret.TextField = new egret.TextField();
  33. tex.width = GameConst.StageW;
  34. tex.textAlign = egret.HorizontalAlign.CENTER;
  35. tex.strokeColor = 0x403e3e;
  36. tex.stroke = 1;
  37. tex.bold = true;
  38. tex.y = GameConst.StageH / 2 + 250;
  39. tex.text = "Powered By ShenYSun";
  40. this.addChild(tex);
  41. }
  42. private onStartGameClick() {
  43. GameControl.Instance.onGameScenesHandler();
  44. }
  45. private onMoreBtnClick() {
  46. console.log("更多游戏");
  47. platform.GetInfo();
  48. }
  49. }

 

点击startBtn按钮执行GameControl类的切换场景方法。

  1. /**游戏管理 */
  2. class GameControl extends egret.Sprite {
  3. private static _instance:GameControl;
  4. public static get Instance() {
  5. if(!GameControl._instance) {
  6. GameControl._instance = new GameControl();
  7. }
  8. return GameControl._instance;
  9. }
  10. /**当前场景 */
  11. private currentStage:egret.DisplayObjectContainer;
  12. //开始游戏
  13. private startGame:StartGameLayer;
  14. /**游戏场景 */
  15. private gameScenes:GameScenesLayer;
  16. /**结束场景 */
  17. private overScenes:GameOverLayer;
  18. /**背景 */
  19. private bgImg:egret.Bitmap;
  20. public constructor() {
  21. super();
  22. this.startGame = new StartGameLayer();
  23. this.gameScenes = new GameScenesLayer();
  24. this.overScenes = new GameOverLayer();
  25. }
  26. public setStageHandler(stage:egret.DisplayObjectContainer):void {
  27. /**设置当前场景的背景 */
  28. this.currentStage = stage;
  29. this.bgImg = GameConst.CreateBitmapByName("bg_jpg");
  30. this.bgImg.width = GameConst.StageW;
  31. this.bgImg.height = GameConst.StageH;
  32. //把背景添加到当期场景
  33. this.currentStage.addChild(this.bgImg);
  34. }
  35. /**开始游戏的场景 */
  36. public startGameHandler():void {
  37. if(this.gameScenes && this.gameScenes.parent) {
  38. GameConst.removeChild(this.gameScenes);
  39. }
  40. if(this.gameScenes && this.overScenes.parent) {
  41. GameConst.removeChild(this.overScenes);
  42. }
  43. this.currentStage.addChild(this.startGame);
  44. GameApp.xia.visible = true;
  45. }
  46. /**游戏场景 */
  47. public onGameScenesHandler():void {
  48. if(this.startGame && this.startGame.parent) {
  49. GameConst.removeChild(this.startGame);
  50. }
  51. if(this.overScenes && this.overScenes.parent) {
  52. GameConst.removeChild(this.overScenes);
  53. }
  54. this.currentStage.addChild(this.gameScenes);
  55. GameApp.xia.visible = false;
  56. }
  57. /**游戏结束场景 */
  58. public showGameOverSceneHandler():void{
  59. if(this.startGame && this.startGame.parent){
  60. GameConst.removeChild(this.startGame)
  61. }
  62. if(this.gameScenes && this.gameScenes.parent){
  63. GameConst.removeChild(this.gameScenes)
  64. }
  65. this.currentStage.addChild(this.overScenes);
  66. GameApp.xia.visible = true;
  67. }
  68. public getGameOverDisplay():GameOverLayer {
  69. return this.overScenes;
  70. }
  71. }

场景切换贯穿游戏全局,封装成类方便调用,以及后期扩展只需要加上新场景类的实例,便可以切换自如。

不难发现上面的开始游戏界面的按钮是MyButton类型,在MyButton类的构造函数中传入背景图和显示文字,创建出一个按钮。此类有一个设置点击事件的方法,按钮调用此公开方法传入触发事件即可设置点击事件。

  1. /**自定义按钮类 */
  2. class MyButton extends egret.Sprite {
  3. private _bg:egret.Bitmap;
  4. private title:egret.Bitmap;
  5. private onClick:Function;
  6. public constructor(bgName:string, titleName:string) {
  7. super();
  8. this._bg = GameConst.createBitmapFromSheet(bgName, "ui");
  9. this.addChild(this._bg);
  10. this.title = GameConst.createBitmapFromSheet(titleName, "ui");
  11. this.title.x = (this._bg.width - this.title.width) >> 1;
  12. this.title.y = (this._bg.height - this.title.height) >> 1;
  13. this.addChild(this.title);
  14. }
  15. //设置点击触发事件
  16. public setClick(func:Function):void {
  17. this.touchEnabled = true;
  18. this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickEvent, this);
  19. this.onClick = func;
  20. }
  21. //点击触发的事件
  22. private onClickEvent() {
  23. this.onClick();
  24. }
  25. public setTitle(title:string):void {
  26. this.title = GameConst.CreateBitmapByName(title);
  27. }
  28. public get bg() {
  29. return this._bg;
  30. }
  31. public set bg(bg:egret.Bitmap) {
  32. this._bg = bg;
  33. }
  34. }

 

一般游戏中的分数、时间等数字组成的UI为了美观都会使用位图文本,但是当游戏逻辑跑起来需要不断的刷新游戏的分数,每次改变分数的时候都要从纹理集里面调用对应位图,在时间上是一个大大的浪费,所以创建一个特殊字符类SpecialNumber,让这个类替我们实现转换特殊字符。

具体代码如下:

  1. /**特殊字符数字类 */
  2. class SpecialNumber extends egret.DisplayObjectContainer {
  3. public constructor() {
  4. super();
  5. }
  6. public gap:number = 0;
  7. /**设置显示的字符串 */
  8. public setData(str:string):void {
  9. this.clear();
  10. if(str == "" || str == null) {
  11. return;
  12. }
  13. //把所有数字每一个都存进数组中
  14. let chars:Array<string> = str.split("");
  15. let w:number = 0;
  16. //所有的长度
  17. let length:number = chars.length;
  18. for(let i:number = 0; i < length; i++) {
  19. try {
  20. let image:egret.Bitmap = GameConst.createBitmapFromSheet(chars[i], "ui");
  21. if(image) {
  22. image.x = w;
  23. w += image.width + this.gap;
  24. this.addChild(image);
  25. }
  26. } catch (error) {
  27. console.log(error);
  28. }
  29. }
  30. this.anchorOffsetX = this.width / 2;
  31. }
  32. public clear() {
  33. while(this.numChildren) {
  34. this.removeChildAt(0);
  35. }
  36. }
  37. }

 

在体验过游戏的时候会发现任务会根据不一样的墙体高度摆不一样的poss,这才poss全是来自于帧动画纹理集,只需要把对应一套的动画解析出来人物就会跳起舞来。下面是人物状态类。

 

人物共有五个状态,其中一个是默认状态state为跳舞状态STAGE1,还有设置当前状态的方法setState

  1. /**角色动作类 */
  2. class Role extends egret.Sprite{
  3. //状态
  4. public static STATE1:number = 0;
  5. public static STATE2:number = 1;
  6. public static STATE3:number = 2;
  7. public static STATE4:number = 3;
  8. public static STATE5:number = 4;
  9. /**人物状态集合 */
  10. public static FRAMES:Array<any> = [
  11. ["0020003", "0020004", "0020005", "0020006","0020007"],
  12. ["0020008"],
  13. ["0020009", "0020010"],
  14. ["0020011", "0020012"],
  15. ["xue0001", "xue0002", "xue0003", "xue0004", "xue0005"]
  16. ]
  17. //身体
  18. private Body:egret.Bitmap;
  19. private state:number;
  20. private currFrames:Array<any>;
  21. private currFramesIndex:number = 0;
  22. private runFlag:number;
  23. private isLoop:boolean;
  24. public constructor() {
  25. super();
  26. this.Body = new egret.Bitmap;
  27. //人物初始状态
  28. this.Body = GameConst.createBitmapFromSheet("Role.FRAMES[0][0]", "Sprites");
  29. //设置锚点
  30. this.Body.anchorOffsetX = this.Body.width * 0.5;
  31. this.addChild(this.Body);
  32. }
  33. /**设置状态 */
  34. public setState(state:number) :void {
  35. this.state = state;
  36. //死亡状态
  37. if(this.state == Role.STATE5) {
  38. this.isLoop = false;
  39. this.Body.anchorOffsetY = this.Body.height * 0;
  40. }else{
  41. this.isLoop = true;
  42. this.Body.anchorOffsetY = this.Body.height * 1;
  43. }
  44. if(this.state == Role.STATE3 || this.state == Role.STATE4){
  45. this.currFrames = [];
  46. if(Math.random() > 0.5){
  47. this.currFrames.push(Role.FRAMES[this.state][0]);
  48. }else{
  49. this.currFrames.push(Role.FRAMES[this.state][1]);
  50. }
  51. }else{
  52. this.currFrames = Role.FRAMES[this.state];
  53. }
  54. this.currFramesIndex = 0;
  55. this.setBody();
  56. }
  57. private setBody() {
  58. this.Body.texture = GameConst.getTextureFromSheet(this.currFrames[this.currFramesIndex], "Sprites");
  59. this.Body.anchorOffsetX = this.Body.width * 0.5;
  60. if(this.state == Role.STATE5){
  61. this.isLoop = false;
  62. this.Body.anchorOffsetY = this.Body.height * 0;
  63. }else{
  64. this.isLoop = true;
  65. this.Body.anchorOffsetY = this.Body.height * 1;
  66. }
  67. }
  68. public run():boolean{
  69. this.runFlag ++;
  70. if(this.runFlag > 4){
  71. this.runFlag = 0;
  72. }
  73. if(this.runFlag != 0){
  74. return;
  75. }
  76. var gotoFrameIndex:number = this.currFramesIndex + 1;
  77. if(gotoFrameIndex == this.currFrames.length){
  78. if(this.isLoop){
  79. gotoFrameIndex = 0;
  80. }else{
  81. gotoFrameIndex = this.currFramesIndex;
  82. }
  83. }
  84. if(gotoFrameIndex != this.currFramesIndex){
  85. this.currFramesIndex = gotoFrameIndex;
  86. this.setBody();
  87. }
  88. return false;
  89. }
  90. public play():void{
  91. egret.startTick(this.run,this);
  92. this.runFlag = 0;
  93. }
  94. public stop():void{
  95. egret.stopTick(this.run,this);
  96. }
  97. }

 

一切工作准备就绪,下面就是本文的重点—–游戏场景的搭建以及逻辑的实现。先看一下游戏内的主要内容

 

首先是蓝色的游戏背景,和开始游戏界面背景如出一辙不用更换,在场景管理的时候注意背景保留一下继续使用。

其次分数、关卡、上背景图等等这些只需要调用常量类的获取纹理集图片的方法调整位置即可实现。

最后重点介绍一下内容:

  • 墙体生成和运动

  • 人物运动和状态切换

  • 分数和关卡数改变并记录最高分数

下面是重要代码片段

 

墙体分别包括上半部分和下半部分

  1. /**上部分墙体容器 */
  2. private topContianer:egret.Sprite;
  3. /**下部分墙体容器 */
  4. private bottomContianer:egret.Sprite;

 

容器内又包含了上下部分的墙体图片,上下边界线

  1. /**上下墙体填充图 */
  2. private topSprite:egret.Sprite;
  3. private bottomSprite:egret.Sprite;
  4. /**上下边界线 */
  5. private topLine:egret.Shape;
  6. private bottomLine:egret.Shape;

 

把填充图和边界线加到容器内(以上边界为例)

  1. this.topContianer = new egret.Sprite();
  2. this.addChild(this.topContianer);
  3. this.topSprite = new egret.Sprite();
  4. this.topContianer.addChild(this.topSprite);
  5. this.topContianer.addChild(this.topLine);

 

定义一个top和bottom范围区间,随机在舞台范围内取值。

  1. let min:number = 150;
  2. let flag:boolean = false;
  3. let len:number = 8;
  4. let w:number = GameConst.StageW / len;
  5. for(let i:number = 0; i < len; i++) {
  6. var h:number = min + Math.floor(Math.random() * 8) * 10;
  7. this.bottomRects.push(new egret.Rectangle(i * w, GameConst.StageH - h, w, h));
  8. h = GameConst.StageH - h;
  9. if (Math.random() < 0.2 || (!flag && i == len - 1)) {
  10. var index:number = Math.floor(Math.random() * this.spaceArr.length);
  11. h -= this.spaceArr[index];
  12. flag = true;
  13. }
  14. this.topRects.push(new egret.Rectangle(i * w, 0, w, h));
  15. }

 

这是随机取区域已经完成,不过都是理想的区域,并没有填充实际上的图片,下面写一个方法通过区域来填充背景墙。

  1. private fullFront(bgSptite:egret.Sprite, rects:Array<egret.Rectangle>, isBottom:boolean = false):void {
  2. bgSptite.cacheAsBitmap = false;
  3. this.clearBg(bgSptite);
  4. var len:number = rects.length;
  5. for (var i:number = 0; i < len; i++) {
  6. var rec:egret.Rectangle = rects[i];
  7. var bitmap:egret.Bitmap;
  8. if (this.bgBitmaps.length) {
  9. bitmap = this.bgBitmaps.pop();
  10. } else {
  11. bitmap = new egret.Bitmap();
  12. bitmap.texture = this.bg;
  13. }
  14. bitmap.scrollRect = rec;
  15. bitmap.x = rec.x;
  16. bitmap.y = rec.y;
  17. bgSptite.addChild(bitmap);
  18. }
  19. }

 

关键代码bitmap.scrollRect = rec是把位图按照区域进行分割,显示对象的滚动矩形范围。显示对象被裁切为矩形定义的大小,当您更改 scrollRect 对象的 x 和 y 属性时,它会在矩形内滚动。

上下背景位图填充完毕,下面可是画上下边界线,同样是写了一个方法(以上边界为例),如下:

  1. private drawLine():void {
  2. var lineH:number = 10;
  3. this.topLine.graphics.clear();
  4. this.topLine.graphics.lineStyle(lineH, 0x33E7FE);
  5. this.bottomLine.graphics.clear();
  6. this.bottomLine.graphics.lineStyle(lineH, 0x33E7FE);
  7. this.drawTopLine(lineH / 2);
  8. this.drawBottomLine(lineH / 2);
  9. this.topLine.graphics.endFill();
  10. this.bottomLine.graphics.endFill();
  11. }
  12. private drawTopLine(lineH:number):void {
  13. var len:number = this.topRects.length;
  14. for (var i:number = 0; i < len; i++) {
  15. var rec:egret.Rectangle = this.topRects[i];
  16. if (i == 0) {
  17. this.topLine.graphics.moveTo(rec.x, rec.height);
  18. this.topLine.graphics.lineTo(rec.x + rec.width, rec.height);
  19. } else {
  20. this.topLine.graphics.lineTo(rec.x, rec.height);
  21. this.topLine.graphics.lineTo(rec.x + rec.width, rec.height);
  22. }
  23. }
  24. }

 

此时,背景填充完毕,但墙体还不能运动。前面this.topContianer.y = -200;把上部分墙体的的纵轴设置在-200的位置,等到游戏开始执行Tween动画,使this.topContianer.y = 0,为了有更好的效果游戏开始延迟1.5s再调用墙体运动,Tween动画如下:

  1. let self = this;
  2. setTimeout(function() {
  3. // self.shakeRun();
  4. //上面的模块往下运动
  5. egret.Tween.get(this.topContianer).to({"y":0}, 100).call(function():void {
  6. self.landOver();
  7. })
  8. }, 1500);

 

人物运动:给舞台添加点击事件,判断点击位置并移动。

  1. /**点击事件 */
  2. private onClick(e:egret.TouchEvent):void {
  3. let len:number = this.bottomRects.length;
  4. for(let i:number = 0; i < len; i++) {
  5. let rec:egret.Rectangle = this.bottomRects[i];
  6. if(e.stageX > rec.x && e.stageX < rec.x + rec.width) {
  7. this.setRolePos(i);
  8. break;
  9. }
  10. }
  11. }

 

操作角色所在位置全部是根据上面定义的人物所在位置下标rolePosIndex的相对位置来决定的。

  1. private setRolePos(index:number, offY:number = 17, offX:number = 0, isInit:boolean = false):void {
  2. if (!isInit) {
  3. //人物每次移动一个格子
  4. if (this.rolePosIndex > index) {
  5. index = this.rolePosIndex - 1;
  6. }
  7. else if (this.rolePosIndex < index) {
  8. index = this.rolePosIndex + 1;
  9. }
  10. }
  11. this.rolePosIndex = index;
  12. var rec:egret.Rectangle = this.bottomRects[index];
  13. //一次只移动一格
  14. this.role.x = rec.x + rec.width / 2 + offX;
  15. this.role.y = rec.y + offY;
  16. }

 

状态切换:

墙体运动完毕之后,通过人物所在位置下标找到上半部分墙体和下半部分墙体对应的位置的差值,并根据差值判断人物是否存活,如果存活应该表现出什么状态。

获取人物所在位置上下墙体的距离:

  1.  
  1. privategetSpace():number{
  2. lettop:egret.Rectangle=this.topRects[this.rolePosIndex];
  3. letbottom:egret.Rectangle=this.bottomRects[this.rolePosIndex];
  4. returnGameConst.StageH-top.height-bottom.height;
  5. }

 

根据返回的距离差值判断人物的状态:

  1.  
  1. privatecheckState() {
  2. letspace:number=this.getSpace();
  3. if(space==0) {
  4. this.role.setState(Role.STATE5);
  5. } elseif(space==this.spaceArr[2]) {
  6. this.role.setState(Role.STATE4);
  7. } elseif(space==this.spaceArr[0]) {
  8. this.role.setState(Role.STATE3);
  9. } elseif(space==this.spaceArr[1]) {
  10. this.role.setState(Role.STATE2);
  11. }
  12. if(space==0) {
  13. this.setRolePos(this.rolePosIndex, -10, 4);
  14. }
  15. }

 

根据返回的距离判断游戏状态,若返回值为0,游戏结束;不为0,进入下一关:

  1. /**检验这关结束主角是否存活 */
  2. privatecheckResult() {
  3. letspace:number=this.getSpace();
  4. letself=this;
  5. if(space==0) {
  6. this.dieNum++;
  7. if(this.dieNum==1) {
  8. this.role.stop();
  9. setTimeout(function() {
  10. //游戏结束
  11. GameControl.Instance.getGameOverDisplay().setGameOverDataHandler(self.score, self.curretMaxScore);
  12. GameControl.Instance.showGameOverSceneHandler();
  13. }, 500);
  14. return;
  15. }
  16. }
  17. //进入下一关
  18. else{
  19. this.curretLevel++;
  20. this.score+=10;
  21. if(this.score>this.curretMaxScore) {
  22. this.curretMaxScore=this.score;
  23. }
  24. //刷新成绩
  25. this.refreshScore();
  26. }
  27. setTimeout(function() {
  28. self.refurbish()
  29. }, 1000);
  30. }

 

接着上一步此时如果人物存活进入下一关,那么就要刷新游戏成绩和关卡数,并检验此时是否为最高成绩:

  1. /**刷新成绩数据 */
  2. privaterefreshScore() {
  3. this.LvNum.setData(this.curretLevel.toString());
  4. this.recodeNum.setData(this.score.toString());
  5. }

 

游戏进入下一关卡:

  1. /**刷新游戏关卡 */
  2. privaterefreshPoint() {
  3. this.initData();
  4. this.start();
  5. }

 

  1.  

游戏结算界面效果图

和开始界面差不多,有所不同的是需要从游戏场景中传入本局分数和做高分数,在这个页面写一个公开的setGameOverDataHandler方法,游戏结束是调用此方法传入数值。

  1. /**游戏结束页面分数最高分数 */
  2. publicsetGameOverDataHandler(score:number=0, maxScore:number=0):void{
  3. this.scoreNum.setData(score.toString());
  4. this.maxScore.setData(maxScore.toString());
  5. }

 

  1.  

游戏场景内当墙体要下落的时候墙体会晃动一下,晃动墙体不仅提醒玩家这个墙体即将下落,同时也增加了这个游戏的可玩性,下面是控制墙体晃动的Shake类。

  1. /**墙体晃动 */
  2. classShake{
  3. privateinitY:number;
  4. privateshakeNum:number;
  5. privateoverFunc:Function;
  6. privateobj:egret.DisplayObject;
  7. privatenum:number;
  8. privateflag:number;
  9. publicrun(obj:egret.DisplayObject, shakeNum:number, overFunc:Function=null) {
  10. this.obj=obj;
  11. this.initY=obj.y;
  12. this.shakeNum=shakeNum;
  13. this.overFunc=overFunc;
  14. egret.startTick(this.loop, this);
  15. this.num=0;
  16. this.flag=0;
  17. }
  18. privateloop():boolean{
  19. if(this.flag==0) {
  20. if(this.obj.y<=this.initY) {
  21. this.obj.y+=5;
  22. } else{
  23. this.obj.y-=5;
  24. }
  25. if(this.obj.y==this.initY) {
  26. this.num++;
  27. if(this.num==this.shakeNum) {
  28. egret.stopTick(this.loop, this);
  29. if(this.overFunc) {
  30. this.overFunc();
  31. }
  32. }
  33. }
  34. }
  35. this.flag++;
  36. if(this.flag==2) {
  37. this.flag=0;
  38. }
  39. returnfalse;
  40. }
  41. }

 

  1. 小结

本文通过对一个简单小游戏进行模块化的分析,并介绍了模块化的好处,降低这些模块之间的耦合度,后期如果要加新功能对旧的代码无需进行大的修改。

版权声明:本文为egret-edcation原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/egret-edcation/p/9728345.html