今天教大家用Egret做一个简单的游戏-《冰桶挑战》,这是一款熟悉的打地鼠游戏,点击屏幕即可用冰桶摧毁从洞里出来的星星,获得结算积分。游戏页面布局使用EUI封装了大量常用的UI组件,满足绝大部分的交互界面需求。开发者仅需一天时间即可独立完成这款小游戏的开发。游戏效果图如下。游戏比较简单,分为以下几个场景:·开始场景·游戏场景·结束场景·游戏开始场景这里使用Eui搭建UI,舞台尺寸为640x960,填充模式为固定宽度;改成640*960,启动资源只有一张图片的场景,拖进去,然后选择top和bottompadding进行约束,把值改成屏幕适配的比例。这里为了看起来像点击“桶”按钮,直接拖一个按钮,然后把它的alpha值改成0;简单代码如下://startsceneclassstartextendseui.Componentimplementseui.UIComponent{publicbtn_Start:eui.Button;公共构造函数(){超级();}protectedpartAdded(partName:string,instance:any):void{super.添加部分(零件名称,实例);}protectedchildrenCreated():void{超级。儿童创建();这个。初始化();}privateInit(){this.height=this.stage.stageHeight;//场景跳转this.btn_Start.addEventListener(egret.TouchEvent.TOUCH_TAP,()={this.parent.addChild(newgame())this.parent.removeChild(this);},this)}}游戏场景拖动背景图片bg_png放入场景中,与起始场景中的背景图片相同,按照调整后的比例进行约束;把一个Groupsize改成舞台的大小,然后上下填充,把它的TouchEnbled改成false;这里由于资源问题,“孔洞”是固定的,所以再添加一组改变字符图像的大小(TouchEnbled也改为false),添加Rect作为遮罩;然后把水桶和倒水的图片拖进去(隐藏这两张图片),结构和效果如下:复制8个小组,按比例约束位置,然后把这9组都隐藏起来;5.代码编写:o打开一个心跳事件protectedchildrenCreated():void{super.childrenCreated();这个。初始化();}privateInit(){egret.startTick(this.Update,this);}//出现的速度privatespeed=1;私人计数=0;//心跳函数privateUpdate():boolean{this.count++;if(this.count==Math.floor(120/this.speed)){this.PeopleChange();这个。计数=0;这个.speed+=0.05;}//判断游戏是否结束if(this.time<=0){this.parent.addChild(newGameOver());egret.stopTick(this.Update,this);this.parent.removeChild(this);}返回假;}update函数写入控制角色出现的速度和游戏结束的判断;privatePeopleChange(){//9组中随机取一个letran=Math.floor(Math.random()*this.group.numChildren);让g:eui.Group=this.group.getChildAt(ran);for(leti=0;i{//easing执行完成后隐藏groupthis.group.getChildAt(i).visible=false;//人物图片位置重置img.y-=img.height;//从组中删除字符图像g.removeChild(img)//移除监视器img.removeEventListener(egret.TouchEvent.TOUCH_TAP,()=>{},this)egret.Tween.removeTweens(img);g.getChildAt(2).visible=false;g.getChildAt(3).visible=false;})人物图片的点击处理img.touchEnabled=true;img.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{//暂停动画tw.setPaused(true)//人物图片不可触摸防止多次点击img.touchEnabled=false;this.tongNum++;GameUtil.Constant.得分++;g.getChildAt(2).visible=true;g.getChildAt(3).visible=true;this.tongNumTXT.text=this.tongNum。toString();//替换浇水图像img.texture=RES.getRes(GameUtil.peopleEnemyWet[random]);//动画200ms后继续播放setTimeout(()=>{tw.setPaused(false);},200);},this)游戏使用简单的计时判断结束,在Init方法中写入//timerlets=setInterval(()=>{if(this.time>0){this.time--;this.timeDownTXT.text=this.time.toString();}else{clearInterval(s);}},1000)结束场景游戏结束场景的构建相对简单。注意按钮和文字也需要按比例进行约束;代码如下://endsceneclassGameOverextendseui.Componentimplementseui.UIComponent{publicbtnPlayAgain:eui.Button;publicbtnFenXiang:eui.Button;publicscoreText:eui.Label;公共构造函数(){超级();}protectedpartAdded(partName:string,instance:any):void{super.partAdded(partName,instance);}protectedchildrenCreated():void{this.height=this.stage.stageHeight;super.childrenCreated();这个.scoreText。text=GameUtil.Constant.score.toString();this.btnPlayAgain.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{this.parent.addChild(newgame());GameUtil.Constant.score=0;this.parent.removeChild(this);},this)}}游戏管理类GameUtil游戏管理类GameUtil包含得分字段和字符图片地址字符串枚举两种状态;moduleGameUtil{exportclassConstant{publicstaticscore:number;}exportenumpeopleEnemy{“person-fs_png”=0,“person-lj_png”,“person-ldh_png”}exportenumpeopleEnemyWet{“person-fs-wet_png”=0,“person-lj-wet_png”,“person-ldh-wet_png"}}总结本文主要讲述一个简单的打地鼠游戏的制作过程。游戏素材均来自网络。游戏的整体结构和功能都比较简单。如果您觉得本文对您有帮助,欢迎在下方评论区留言与我们互动!GitHub源码地址:https://github.com/duan003387...