Egret初体验–躲避类小游戏
下面简单介绍一下我这个游戏:
基本上就3个画面(准备再添加一个胜利的界面)
开始画面,一个按钮,点击进入游戏
游戏画面,滚动的背景,触摸移动的老鹰,从天而降的翔,以及右上角的时间条
结束画面,显示结果,关注按钮和重玩一次按钮

游戏主文件:GameContainer.ts(游戏逻辑)
4个类文件:GameUtil.ts(功能集合),BgMap.ts(背景滚动),Shit.ts(翔的创建和回收),ScorePanel.ts(结果展示)
/**GameUtil.ts*/
/**基于圆心的碰撞检测*/
public static hitTestP(obj1:egret.DisplayObject,obj2:egret.DisplayObject):boolean
{
var rect2x:number;
var rect2y:number;
rect2x = obj2.x + obj2.width/2;
rect2y = obj2.y + obj2.height/2;
return obj1.hitTestPoint(rect2x, rect2y);
}
/**根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。*/
export function createBitmapByName(name:string):egret.Bitmap {
var result:egret.Bitmap = new egret.Bitmap();
var texture:egret.Texture = RES.getRes(name);
result.texture = texture;
return result;
}
/**BgMap.ts*/
/**可滚动的背景*/
export class BgMap extends egret.DisplayObjectContainer
{
/**图片引用*/
private bmpArr:egret.Bitmap[];
/**图片数量*/
private rowCount:number;
/**stage宽*/
private stageW:number;
/**stage高*/
private stageH:number;
/**纹理本身的高度*/
private textureHeight:number;
/**控制滚动速度*/
private speed:number = 4;
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
}
/**初始化*/
private onAddToStage(event:egret.Event){
this.removeEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
this.stageW = this.stage.stageWidth;
this.stageH = this.stage.stageHeight;
var texture:egret.Texture = RES.getRes("bgImage");
this.textureHeight = texture.textureHeight;//保留原始纹理的高度,用于后续的计算
this.rowCount = Math.ceil(this.stageH/this.textureHeight)+1;//计算在当前屏幕中,需要的图片数量
this.bmpArr = [];
//创建这些图片,并设置y坐标,让它们连接起来
for(var i:number=0;i<this.rowCount;i++)
{
var bgBmp:egret.Bitmap = AvoidShit.createBitmapByName("bgImage");
bgBmp.y = this.textureHeight*i-(this.textureHeight*this.rowCount-this.stageH);
this.bmpArr.push(bgBmp);
this.addChild(bgBmp);
}
}
/**开始滚动*/
public start():void {
this.removeEventListener(egret.Event.ENTER_FRAME,this.enterFrameHandler,this);
this.addEventListener(egret.Event.ENTER_FRAME,this.enterFrameHandler,this);
}
/**逐帧运动*/
private enterFrameHandler(event:egret.Event):void {
for(var i:number=0;i<this.rowCount;i++)
{
var bgBmp:egret.Bitmap = this.bmpArr[i];
bgBmp.y+=this.speed;
//判断超出屏幕后,回到队首,这样来实现循环反复
if(bgBmp.y > this.stageH) {
bgBmp.y = this.bmpArr[0].y-this.textureHeight;
this.bmpArr.pop();
this.bmpArr.unshift(bgBmp);
}
}
}
/**暂停滚动*/
public pause():void {
this.removeEventListener(egret.Event.ENTER_FRAME,this.enterFrameHandler,this);
}
}
/**Shit.ts*/
/**大便,利用对象池*/
export class Shit extends egret.Bitmap
{
private static cacheDict:Object = {};
/**生产*/
public static produce(textureName:string):AvoidShit.Shit
{
if(AvoidShit.Shit.cacheDict[textureName]==null)
AvoidShit.Shit.cacheDict[textureName] = [];
var dict:AvoidShit.Shit[] = AvoidShit.Shit.cacheDict[textureName];
var shit:AvoidShit.Shit;
if(dict.length>0) {
shit = dict.pop();
} else {
shit = new AvoidShit.Shit(RES.getRes(textureName));
}
shit.textureName = textureName;
return shit;
}
/**回收*/
public static reclaim(shit:AvoidShit.Shit,textureName:string):void
{
if(AvoidShit.Shit.cacheDict[textureName]==null)
AvoidShit.Shit.cacheDict[textureName] = [];
var dict:AvoidShit.Shit[] = AvoidShit.Shit.cacheDict[textureName];
if(dict.indexOf(shit)==-1)
dict.push(shit);
}
public textureName:string;
public constructor(texture:egret.Texture) {
super(texture);
}
}
/**ScorePanel.ts*/
/**成绩显示*/
export class ScorePanel extends egret.Sprite
{
private txt:egret.TextField;
public constructor() {
super();
var g:egret.Graphics = this.graphics;
g.drawRect(0,0,100,100);
g.endFill();
this.txt = new egret.TextField();
this.txt.width = 100;
this.txt.height = 100;
this.txt.textAlign = "center";
this.txt.textColor = 0x00dddd;
this.txt.size = 24;
this.txt.y = 60;
this.addChild(this.txt);
this.touchChildren = false;
this.touchEnabled = false;
}
public showScore(value:number):void {
var msg:string = value+"";
this.txt.text = msg;
}
}
上面4个源码基本是基于官方的Demo而来,下面是游戏逻辑的代码:(这个代码太长就只贴主要部分)
游戏初始画面就是把标题,按钮都堆积在上面,比较简单,给按钮增加一个点击事件的监听就行了
this.icon.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gameStart, this);
游戏进行界面,主要有三点:时间轴,翔,碰撞检测(GameUtil做了,直接用就行)
时间轴有三部分:背景框,中间的轴,遮罩层。
/**背景框*/
this.timelinebg = AvoidShit.createBitmapByName("tlbgImage");
this.timelinebg.x = this.stageW - this.timelinebg.width - 10;
this.timelinebg.y = 10;
this.addChild(this.timelinebg);
/**中间的轴*/
this.timeline = AvoidShit.createBitmapByName("tlImage");
this.timeline.x = this.stageW - this.timeline.width - 13;
this.timeline.y = 14;
this.addChild(this.timeline);
/**遮罩层*/
this.timelinemask = new egret.Rectangle(0, 0, this.timeline.width, this.timeline.height);
this.timeline.mask = this.timelinemask;
/*计时器增加遮罩层刷新的方法*/
this.gameTimer = new egret.Timer(this.timeDelay,this.timeCount);
this.gameTimer.addEventListener(egret.TimerEvent.TIMER, this.timelineDown, this);
/**遮罩层移动*/
private timelineDown(evt:egret.TimerEvent):void {
this.timelinemask.y += this.timeline.height/20;
}
翔的产生和销毁都是调用Shit.ts中的方法
/**创建大便*/
private createShit(evt:egret.TimerEvent):void{
var shit:AvoidShit.Shit = AvoidShit.Shit.produce("shitImage");
shit.x = Math.random()*(this.stageW-shit.width);
shit.y = -shit.height-Math.random()*300;
this.addChildAt(shit,this.numChildren-1);
this.shits.push(shit);
}
//大便运动
var theShit:AvoidShit.Shit;
var enemyFighterCount:number = this.shits.length;
for(i=0;i<enemyFighterCount;i++) {
theShit = this.shits[i];
theShit.y += this.downTimes * speedOffset;
if(theShit.y>this.stageH)
delArr.push(theShit);
}
for(i=0;i<delArr.length;i++) {
theShit = delArr[i];
this.removeChild(theShit);
AvoidShit.Shit.reclaim(theShit,"shitImage");
this.shits.splice(this.shits.indexOf(theShit),1);
}
游戏结束有两个条件,一是时间到了,二是碰到翔
结束画面就是展示成绩和提供两个按钮
最主要的应该是微信分享功能:
需要用到两个文件libs/WeixinAPI.d.ts和launcher/WeixinAPI.js
并在html页面中增加调用
private doShare(n:number,m:number) {
WeixinApi.ready(function(api:WeixinApi) {
var info:WeixinShareInfo = new WeixinShareInfo();
info.title = "我本卖萌鸟,何处惹尘埃?";
if (m == 0) {
info.desc = "我本卖萌鸟,何处惹尘埃?";
} else {
info.desc = "我本卖萌鸟,何处惹尘埃?屎开,屎开~~我躲过了" + n + "坨大便,你行吗?";
}
info.link = "http://games.11wj.com/fly/launcher/release.html";
info.imgUrl = "http://games.11wj.com/fly/resource/assets/icon.png";
api.shareToFriend(info);
api.shareToTimeline(info);
})
}
Egret初体验–躲避类小游戏的更多相关文章
- WEBGL学习笔记(七):实践练手1-飞行类小游戏之游戏控制
接上一节,游戏控制首先要解决的就是碰撞检测了 这里用到了学习笔记(三)射线检测的内容了 以鸟为射线原点,向前.上.下分别发射3个射线,射线的长度较短大概为10~30. 根据上一节场景的建设,我把y轴设 ...
- [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏
前言 这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示 ...
- C++ MFC棋牌类小游戏day1
好用没用过C++做一个完整一点的东西了,今天开始希望靠我这点微薄的技术来完成这个小游戏. 我现在的水平应该算是菜鸟中的战斗鸡了,所以又很多东西在设计和技术方面肯定会有很大的缺陷,我做这个小游戏的目的单 ...
- C++ MFC棋牌类小游戏day6
双人单机小游戏做完了,规则那部分还没介绍,暂时不打算介绍了,因为写的这个bug太多,我打算重新修改. 链接:https://pan.baidu.com/s/1XQKPSv0Tw36Qi2TeaRJiM ...
- C++ MFC棋牌类小游戏day5
先整理一下之前的内容: 1.画了棋盘,把棋盘的每个点的状态都保存起来. 2.画棋子,分别用tiger类和people类画了棋子,并且保存了棋子的初始状态. 下面开始设计棋子的移动: 1.单机棋子,选中 ...
- C++ MFC棋牌类小游戏day4
根据昨天的计划,今天开始做下面的内容. 1.鼠标点击事件 2.点击坐标进行处理.(坐标转换) 3.判断选中的位置是否有效. 4.确定选中的棋子,设置棋子的状态和棋子所在坐标的状态. 5.判断移动是否有 ...
- C++ MFC棋牌类小游戏day3
今天开始设计小人棋子. 画法跟画虎一样,唯一不一样的是小人在刚开始会有重叠的情况,所以画起来可能比虎的棋子能够难一点. 我打算用Location结构体中的num来标记每个棋盘坐标存在棋子的个数,isH ...
- C++ MFC棋牌类小游戏day2
反思了一下昨天的设计,觉得略有不足,我决定把棋盘做成单例模式.这样的话需要重新设计棋盘类,emmm,是新建棋盘类. Baord类 成员变量: Location coordinate;//棋子坐标 b ...
- 使用Laya引擎开发微信小游戏(上)
本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...
随机推荐
- poI地址纠偏查询存储过程理解
1.POI存储过程查询 服务器类型:PostgreSQL function name 函数名字 address_parse_no_save(lon float8, lat float8) DECLAR ...
- VRay 2.0 SP1 2.10.01 for 3ds max 9/2008/2009/2010/2011/2012 32/64位 顶渲简体中文版+英文版[中国室内设计论坛-室内人]
VRay 2.0 SP1 2.10.01 for 3ds max 9/2008/2009/2010/2011/2012 32/64位 顶渲简体中文版+英文版[中国室内设计论坛-室内人] 对最新版本的V ...
- CURL使用HTTPS的技术小结
摘自http://www.51testing.com/html/14/175414-248202.html CURL使用HTTPS的技术小结 cURL是linux下命令行提交HTTP(S)请求的一个很 ...
- #include <set>
1 multiset 多重集合(multiset) 允许存在两个次序相等的元素的集合 <set> 2 set 集合(set) 由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作 ...
- 使用View Model从表现层分离领域模型
本文来自:http://www.cnblogs.com/shanyou/archive/2010/04/03/1703501.html Model-View-Controller(模型-视图-控制器, ...
- jQuery中设置form表单中action值的方法
jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈 html代码: <form id="myFormId&quo ...
- C语言高速入口系列(七)
C语言高速入口系列(七) C语言指针进阶 本章引言: 在前面第5节中我们对C语言的指针进行了初步的学习理解;作为C语言的灵魂, C指针肯定没那么简单,在这一节中,我们将会对指针进行进一步的学习,比方二 ...
- Hadoop基础
Hadoop组成 包括两个核心组成:HDFS:分布式文件系统,存储海量的数据MapReduce:并行处理框架,实现任务分解和调度 搭建大型数据仓库,PB级数据的存储.处理.分析.统计等业务(搜索引擎. ...
- webapi拦截请求
[AttributeUsage(AttributeTargets.Method)] public class WebApiSensitive : ActionFilterAttribute { pub ...
- HTML及CSS常用颜色英文词汇
黑色 银色 灰色 白色 茶色 红色 紫色 紫红 black silver gray white maroon red purple fuchsia #000000 #C0C0C0 #808080 #F ...