下面简单介绍一下我这个游戏:
基本上就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初体验–躲避类小游戏的更多相关文章

  1. WEBGL学习笔记(七):实践练手1-飞行类小游戏之游戏控制

    接上一节,游戏控制首先要解决的就是碰撞检测了 这里用到了学习笔记(三)射线检测的内容了 以鸟为射线原点,向前.上.下分别发射3个射线,射线的长度较短大概为10~30. 根据上一节场景的建设,我把y轴设 ...

  2. [安卓] 12、开源一个基于SurfaceView的飞行射击类小游戏

    前言  这款安卓小游戏是基于SurfaceView的飞行射击类游戏,采用Java来写,没有采用游戏引擎,注释详细,条理比较清晰,适合初学者了解游戏状态转化自动机和一些继承与封装的技巧. 效果展示    ...

  3. C++ MFC棋牌类小游戏day1

    好用没用过C++做一个完整一点的东西了,今天开始希望靠我这点微薄的技术来完成这个小游戏. 我现在的水平应该算是菜鸟中的战斗鸡了,所以又很多东西在设计和技术方面肯定会有很大的缺陷,我做这个小游戏的目的单 ...

  4. C++ MFC棋牌类小游戏day6

    双人单机小游戏做完了,规则那部分还没介绍,暂时不打算介绍了,因为写的这个bug太多,我打算重新修改. 链接:https://pan.baidu.com/s/1XQKPSv0Tw36Qi2TeaRJiM ...

  5. C++ MFC棋牌类小游戏day5

    先整理一下之前的内容: 1.画了棋盘,把棋盘的每个点的状态都保存起来. 2.画棋子,分别用tiger类和people类画了棋子,并且保存了棋子的初始状态. 下面开始设计棋子的移动: 1.单机棋子,选中 ...

  6. C++ MFC棋牌类小游戏day4

    根据昨天的计划,今天开始做下面的内容. 1.鼠标点击事件 2.点击坐标进行处理.(坐标转换) 3.判断选中的位置是否有效. 4.确定选中的棋子,设置棋子的状态和棋子所在坐标的状态. 5.判断移动是否有 ...

  7. C++ MFC棋牌类小游戏day3

    今天开始设计小人棋子. 画法跟画虎一样,唯一不一样的是小人在刚开始会有重叠的情况,所以画起来可能比虎的棋子能够难一点. 我打算用Location结构体中的num来标记每个棋盘坐标存在棋子的个数,isH ...

  8. C++ MFC棋牌类小游戏day2

    反思了一下昨天的设计,觉得略有不足,我决定把棋盘做成单例模式.这样的话需要重新设计棋盘类,emmm,是新建棋盘类. Baord类 成员变量: Location  coordinate;//棋子坐标 b ...

  9. 使用Laya引擎开发微信小游戏(上)

    本文由云+社区发表 使用一个简单的游戏开发示例,由浅入深,介绍了如何用Laya引擎开发微信小游戏. 作者:马晓东,腾讯前端高级工程师. 微信小游戏的推出也快一年时间了,在IEG的游戏运营活动中,也出现 ...

随机推荐

  1. SQL Server 2008数据库的一些基本概念 区、页、行

    原文地址:http://www.cnblogs.com/liuzhendong/archive/2011/10/11/2207361.html 以前总是没弄明白这些基本概念,现在整理如下: 1.区: ...

  2. C++数据结构之最小生成树

    最小生成树是图的一部分,一般求最小生成树用Prim算法和Kruskal算法. 对于Prim算法,思想是:在访问过的顶点和未访问的顶点之间选择权值最小的边.Prim算法是基于顶点的操作,适合于顶点较少, ...

  3. MediaInfo使用简介(新版本支持HEVC)

    MediaInfo 用来分析视频和音频文件的编码和内容信息,是一款是自由软件 (免费使用.免费获得源代码).他除了提供DLL之外,本身也提供GUI工具用于查看视频信息.我使用中发现,新版本的Media ...

  4. Android面试题目及其答案

    转自:http://blog.csdn.net/wwj_748/article/details/8868640 Android面试题目及其答案 1.Android dvm的进程和Linux的进程, 应 ...

  5. JS框架~Angularjs

    无意中看到anytao的项目,工作台,使用了Angularjs框架,感觉在前端表现上用户体验比较好,于是就简单看了一下,原来使用很简单,或者说,人家把代码封装的很好,以至于开发人员调用时比较简单,呵呵 ...

  6. 小米手机与魅族的PK战结果 说明了什么

    我国电子商务面临的问题,淘宝退出百度无疑是一个遗憾.当在网上购物时.用户面临的一个非常大的问题就是怎样在众多的站点找到自己想要的物品,并以最低的价格买到.自从淘宝退出百度.建立自己的搜索引擎后,广大消 ...

  7. 手工删除oracle的方法

    大致方法如下:  1.删除物理文件     1.1.oracle安装文件.     1.2.系统目录下,program files文件下的oracle文件 2.注册表中大概有这么几个地方:  hkey ...

  8. 代理方法keywordAction与Fun的使用

    代理是一种特殊的,指向某个方法模块所在的地址.一般来讲,那个方法模块,能够是一个普通的方法,很多其它的时候,是一团匿名的lamda表达式,即一个匿名方法.如今简单理解一下代理的简写方式,即Action ...

  9. Http权威指南笔记(一) URI URL URN 关系

    定义 URI:统一资源标识符(Uniform Resource Indentifier)用来标识服务器上的资源. URL:统一资源定位符(Uniform Resouce Locator)是资源标识符最 ...

  10. Windows SQL Server 2012 R2 安装Intel I217-V/I218-V网卡驱动(转)

    1.下载Intel官方驱动: https://downloadcenter.intel.com/Detail_Desc.aspx?agr=Y&DwnldID=23071&lang=zh ...