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的游戏运营活动中,也出现 ...
随机推荐
- 关于App的一些迷思以及一些动画效果开源库的推荐
http://www.open-open.com/lib/view/open1427856817396.html
- HDU 4721 Food and Productivity (二分+树状数组)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :给出n * m的格子,每个格子有两个属性f ...
- linux下如何产生core,调试core
linux下如何产生core,调试core 摘自:http://blog.163.com/redhumor@126/blog/static/19554784201131791239753/ 在程序不寻 ...
- Android九宫格图片(9.png)的讲解与制作
刚开始学习Android的时候,会见到res/drawable的几个文件里面有*.9.png格式命名的图片文件.起初以为这只是Android素材的一些特殊命名,其实不是.它是能实现图片素材拉伸.收缩不 ...
- SNMP协议具体解释
简单网络管理协议(SNMP)是TCP/IP协议簇的一个应用层协议.在1988年被制定,并被Internet体系结构委员会(IAB)採纳作为一个短期的网络管理解决方式:因为SNMP的简单性,在Inter ...
- SQL Server DML(UPDATE、INSERT、DELETE)常见用法(一)
1.引言 T-SQL(Transact Structured Query Language)是标准的SQL的扩展,是程序和SQL Server沟通的主要语言. T-SQL语言主要由以下几部分组成: 数 ...
- document load 与document ready的区别
页面加载完成有两种事件 1.load是当页面所有资源全部加载完成后(包括DOM文档树,css文件,js文件,图片资源等),执行一个函数 问题:如果图片资源较多,加载时间较长,onload后等待执行的函 ...
- JS中的Function对象
Function是函数的原型,所有的函数都来源于Function,获得函数的方法有两种类型,分为动态函数和函数继承. 动态函数 创建一个Function语法: var func = new Funct ...
- Emmet 插件使用教程
1)使用 Emmet 生成 HTML 的语法详解生成 HTML 文档初始结构 HTML 文档的初始结构,就是包括 doctype.html.head.body 以及 meta 等内容.你只需要输入一个 ...
- springmvc jstl
springmvc运用maven的jetty插件运行成功,部署在tomcat6报错:ClassNotFoundException: javax.servlet.jsp.jstl.core.Config ...