Starling开发微信打灰机(二)
上一篇中,已经把starling开发环境搭建好,那么现在开始写代码。
这一篇来完成打灰机的欢迎界面。
游戏素材下载
首先创建Asset.as来加载图片,声音和字体等资源,其中只有两张背景图是单独的图片,其他图片都已经被我用TexturePacker发布成序列了,所以只需要嵌入进去就好。
Assets.as
public class Assets
{ [Embed(source="../assets/bg_01.jpg")]
private static const Bg01:Class; [Embed(source="../assets/bg_02.jpg")]
private static const Bg02:Class; [Embed(source = "../assets/plane.png")]
private static const PlanePng:Class; [Embed(source = "../assets/plane.xml", mimeType = "application/octet-stream")]
private static const PlaneXml:Class; [Embed(source = "../assets/font_0.png")]
private static const FontPng:Class; [Embed(source = "../assets/font.fnt", mimeType = "application/octet-stream")]
private static const FontXml:Class; [Embed(source = "../assets/shoot.mp3")]
private static const ShootMp3:Class; [Embed(source = "../assets/explosion.mp3")]
private static const ExplosionMp3:Class; public static var bg01Txr:Texture;
public static var bg02Txr:Texture;
public static var atlas:TextureAtlas;
public static var shootSound:Sound;
public static var explosionSound:Sound; public static function init():void
{
bg01Txr = Texture.fromBitmap(new Bg01());
bg02Txr = Texture.fromBitmap(new Bg02());
atlas = new TextureAtlas(Texture.fromBitmap(new PlanePng()), XML(new PlaneXml()));
TextField.registerBitmapFont(new BitmapFont(Texture.fromBitmap(new FontPng()), XML(new FontXml())));
shootSound = new ShootMp3() as Sound;
shootSound.play(0, 0, new SoundTransform(0));
explosionSound = new ExplosionMp3() as Sound;
explosionSound.play(0, 0, new SoundTransform(0));
trace("assets loaded.");
}
}
完了之后就开始做第一个欢迎界面,上面显示四个大字“飞机大战”,还有其他就自己发挥了。
在创建第一个显示界面之前,先考虑游戏中可能存在的几种状态,即:欢迎界面,游戏界面,暂停界面,可能还需要显示分数的界面。
为了程序结构性,最好为这些状态界面创建公共接口IState.as
public interface IState
{
function update():void; function destroy():void;
}
完了之后来创建欢迎界面。 注意 这里就要用到starling.display.Sprite类,而启动类Main.as则是继承flash.display.Sprite。
Welcome.as
public class Welcome extends Sprite implements IState
{
private var game:Game;
private var bg:Background;
private var title:TextField;
private var author:TextField;
private var finalScore:TextField; public function Welcome(game:Game, score:Number)
{
this.game = game; bg = new Background();
addChild(bg); title = new TextField(Config.STAGE_WIDTH*0.95, 80, "飞机大战", "children", 64, 0xff0000, true);
title.hAlign = "center";
title.pivotX = title.width / 2;
title.x = Config.STAGE_WIDTH / 2;
title.y = Config.STAGE_HEIGHT / 2 - 100;
addChild(title); author = new TextField(Config.STAGE_WIDTH*0.95, 80, "by小强", "children", 42, 0xff0000, true);
author.hAlign = "center";
author.pivotX = author.width / 2;
author.x = Config.STAGE_WIDTH / 2;
author.y = title.y + 100;
addChild(author); addEventListener(TouchEvent.TOUCH, onTouchScreen);
} public function update():void
{
bg.update();
} private function onTouchScreen(event:TouchEvent):void
{
var touch:Touch = event.getTouch(this, TouchPhase.BEGAN);
if (touch) {
var touchPos:Point = touch.getLocation(this);
//game.alterState(new Play(game));//游戏开始
}
} public function destroy():void
{
removeFromParent();
}
}
上面的代码向屏幕中添加背景和两个显示文本,使用的字体是"children",该字体是在Assets.as注册的bitmap字体。
其中还为sprite注册了touch事件,以监听手机上面的touch 动作,starling中的touch event使用方法请参考:http://wiki.starling-framework.org/manual/touch_events
Background.as
public class Background extends Sprite
{
public static const BG_RATE:Number = 2; private var bg01:Image;
private var bg02:Image; public function Background()
{
bg01 = new Image(Assets.bg01Txr);
bg01.blendMode = BlendMode.NONE;
bg01.scaleX = Config.STAGE_WIDTH / bg01.width;
bg01.scaleY = Config.STAGE_HEIGHT / bg01.height; bg02 = new Image(Assets.bg02Txr);
bg02.blendMode = BlendMode.NONE;
bg02.y = -Config.STAGE_HEIGHT;
bg02.scaleX = Config.STAGE_WIDTH / bg02.width;
bg02.scaleY = Config.STAGE_HEIGHT / bg02.height; addChild(bg01);
addChild(bg02);
} public function update():void
{
bg01.y += BG_RATE;
if (bg01.y == Config.STAGE_HEIGHT) {
bg01.y = -Config.STAGE_HEIGHT;
} bg02.y += BG_RATE;
if (bg02.y == Config.STAGE_HEIGHT) {
bg02.y = -Config.STAGE_HEIGHT;
} }
}
背景sprite中主要是使得两张背景图一前一后的向下移动,Config是我定义的一个配置类,其中STAGE_WIDTH,STAGE_HEIGHT是舞台宽度和高度。
最后更改之前的Game.as,将Welcome界面创建并添加到stage
public class Game extends Sprite
{ public var currState:IState;
public var loading:Loading;
public function Game()
{
addEventListener(Event.ADDED_TO_STAGE, init);
} private function init(event:Event):void
{
Assets.init(); //显示欢迎界面
currState = new Welcome(this, 0);
addChild(Sprite(currState)); addEventListener(Event.ENTER_FRAME, update);
} private function update(event:Event):void
{
currState.update();
} public function alterState(activeState:IState):void
{
currState.destroy();
currState = activeState;
addChild(Sprite(currState));
}
}
Game类是一个总的控制类,他的public方法alterState接收一个IState类型参数用于改变当前在update的界面。当游戏开始由欢迎界面转到游戏界面,游戏界面转到得分界面都需要调用该方法,使得切换当前update的界面。IState接口设计的好处就在于不Game类不需要知道当前是哪个具体的state,只需要调用其共有方法。
最后以手机应用程序运行:
如果你的android手机打开了USB调试,并且连接到了你的电脑,那在启动方法那里,你可以选择‘在设备上’,稍等片刻flash builder就会将程序自动安装到你的手机上,并打开。 当starling中使用了touch event的时候,最好选择该方法调试。
源码下载:
Starling开发微信打灰机(二)的更多相关文章
- Starling开发微信打灰机(一)
Starling是一个开源的flash游戏开发框架,它能使用flash player 11的gpu加速来使得游戏更加流畅,同事它也支持手机触摸事件,开发者也能用它很轻易地开发手机小游戏. 更多star ...
- Java开发微信公众号(二)---开启开发者模式,接入微信公众平台开发
接入微信公众平台开发,开发者需要按照如下步骤完成: 1.填写服务器配置 2.验证服务器地址的有效性 3.依据接口文档实现业务逻辑 资料准备: 1.一个可以访问的外网,即80的访问端口,因为微信公众号接 ...
- 使用Java开发微信公众平台(二)——消息的接收与响应
上一篇文章(http://www.jerehedu.com/fenxiang/171807_for_detail.htm )中,我们学习了使用Java语言开发微信公众平台的第一部分——环境搭建与开发接 ...
- C#开发微信小程序(二)
导航:C#开发微信小程序系列 关于小程序项目结构,框架介绍,组件说明等,请查看微信小程序官方文档,关于以下贴出来的代码部分我只是截取了一些片段,方便说明问题,如果需要查看完整源代码,可以在我的项目库中 ...
- WordPress 网站开发“微信小程序“实战(二)
原文链接:https://devework.com/wordpres...,转载请用明链注明来源,谢谢! 本文是"WordPress 开发微信小程序"系列的第二篇,本文记录的是开发 ...
- Jfinal极速开发微信系列教程(二)--------------让微信公众平台通过80端口访问本机
概述: 微信公众平台要成为开发者,需要填写接口配置信息中的“URL”和“Token”这两项(参见:http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E ...
- NodeJs 开发微信公众号(二)测试环境部署
由于卤煮本人是做前端开发的,所以在做公众号过程中基本上没有遇到前端问题,在这方面花的时间是最少的.加上用了mui框架(纯css界面)和自己积累的代码,很快地开发出了界面来.接着是后台开发.卤煮选的是n ...
- PHP开发微信公众号(二)消息接受与推送
上一篇文章我们知道怎么获取二维码,这样别人就可以扫描二维码来关注我们,但是别人关注后,发送消息,我们怎么进行相关处理? 这里我们就来学习下怎么处理处理这些消息,以及推送消息. 学习之前首先你需要有一个 ...
- Delphi 开发微信公众平台 (二)- 用户管理
一.用户标签管理 开发者可以使用用户标签管理的相关接口,实现对公众号的标签进行创建.查询.修改.删除等操作,也可以对用户进行打标签.取消标签等操作. 1.创建标签 /// <summary> ...
随机推荐
- Java iText5.5.1 绘制PDF表格
iText下载链接:http://sourceforge.net/projects/itext/files/ 会有两个文件夹:extrajars中的extrajars-2.3.jar文件用于解决中文不 ...
- 刺猬大作战(游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4)
游戏特性[编辑] 游戏引擎用Free Pascal写成,GUI用C++写成,使用SDL和Qt4[2]. 0.9.12开始支持实时动态缩放游戏画面. 个性化[编辑] 刺猬大作战有着高度定制性 游戏模式: ...
- HtmlAgilityPack - 简介
HtmlAgilityPack是.net下的一个HTML解析类库.支持用XPath来解析HTML.这个意义不小,为什么呢?因为对于页面上的元素的xpath某些强大的浏览器能够直接获取得到,并不需要手动 ...
- UESTC_菲波拉契数制 2015 UESTC Training for Dynamic Programming<Problem E>
E - 菲波拉契数制 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_Islands 2015 UESTC Training for Data Structures<Problem J>
J - Islands Time Limit: 30000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Su ...
- Add Digits 解答
Question Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- SQL 通配符
在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符.SQL 通配符必须与 LIKE 运算符一起使用,必须放在引号内. 在 SQL 中,可使用以下通配符: %:替代一个或多个字符. _:仅替代 ...
- qsort的几种用法
#include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b){ return *(int ...
- DataReader、Table、DataSet和Entity相互转化
public class CommonService { #region DataReader转化 /// <summary> /// 将DataReader转化为Table /// &l ...
- Lazy方式单列模式,一种线程安全模式的新选择
public class WeProxyClient { private static readonly Lazy<WeProxyClient> list = new ...