class Main extends eui.UILayer {
/**
* 加载进度界面
* loading process interface
*/
/*
* 汽车的状态 0代表向左 , 1代表向右
*/
private loadingView: LoadingUI;
public fox:egret.Bitmap;
public car:egret.Bitmap[];
public m:number[];
// 申请计数器
public win : number ;
public lose : number;
// 文本类
public tj : egret.TextField ;
protected createChildren(): void {
super.createChildren();
egret.lifecycle.addLifecycleListener((context) => {
// custom lifecycle plugin
})
egret.lifecycle.onPause = () => {
egret.ticker.pause();
}
egret.lifecycle.onResume = () => {
egret.ticker.resume();
}
//inject the custom material parser
//注入自定义的素材解析器
let assetAdapter = new AssetAdapter();
egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
//Config loading process interface
//设置加载进度界面
this.loadingView = new LoadingUI();
this.stage.addChild(this.loadingView);
// initialize the Resource loading library
//初始化Resource资源加载库
RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
RES.loadConfig("resource/default.res.json", "resource/");
}
/**
* 配置文件加载完成,开始预加载皮肤主题资源和preload资源组。
* Loading of configuration file is complete, start to pre-load the theme configuration file and the preload resource group
*/
private onConfigComplete(event: RES.ResourceEvent): void {
RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
// load skin theme configuration file, you can manually modify the file. And replace the default skin.
//加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
let theme = new eui.Theme("resource/default.thm.json", this.stage);
theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
RES.loadGroup("preload");
}
private isThemeLoadEnd: boolean = false;
/**
* 主题文件加载完成,开始预加载
* Loading of theme configuration file is complete, start to pre-load the
*/
private onThemeLoadComplete(): void {
this.isThemeLoadEnd = true;
this.createScene();
}
private isResourceLoadEnd: boolean = false;
/**
* preload资源组加载完成
* preload resource group is loaded
*/
private onResourceLoadComplete(event: RES.ResourceEvent): void {
if (event.groupName == "preload") {
this.stage.removeChild(this.loadingView);
RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
this.isResourceLoadEnd = true;
this.createScene();
}
}
private createScene() {
if (this.isThemeLoadEnd && this.isResourceLoadEnd) {
this.startCreateScene();
}
}
/**
* 资源组加载出错
* The resource group loading failed
*/
private onItemLoadError(event: RES.ResourceEvent): void {
console.warn("Url:" + event.resItem.url + " has failed to load");
}
/**
* 资源组加载出错
* Resource group loading failed
*/
private onResourceLoadError(event: RES.ResourceEvent): void {
//TODO
console.warn("Group:" + event.groupName + " has failed to load");
//忽略加载失败的项目
//ignore loading failed projects
this.onResourceLoadComplete(event);
}
/**
* preload资源组加载进度
* loading process of preload resource
*/
private onResourceProgress(event: RES.ResourceEvent): void {
if (event.groupName == "preload") {
this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
}
}
private textfield: egret.TextField;
/**
* 创建场景界面
* Create scene interface
*/
protected startCreateScene(): void {
let bj:egret.Bitmap = this.createBitmapByName('beijing_jpg');
this.addChild(bj);
this.fox = this.createBitmapByName('huli_png');
this.addChild(this.fox);
this.fox.anchorOffsetX = this.fox.width / 2;
this.fox.anchorOffsetY = this.fox.height / 2;
this.fox.x = 300;
this.fox.y = 1000;
// 初始化胜利和死亡次数
this.win = this.lose = 0;
// 初始化计数器
this.tj = new egret.TextField();
this.addChild(this.tj);
this.tj.size = 40 ;
this.tj.textColor = 0xff0000;
// 干扰元素
this.car = [];
this.m = [];
for(var i:number=0;i<5;i++){
this.car[i] = this.createBitmapByName('qiche2_png');
this.addChild(this.car[i]);
this.car[i].anchorOffsetX = this.car[i].width / 2;
this.car[i].anchorOffsetY = this.car[i].height / 2;
this.car[i].x = Math.random() * 720;
this.car[i].y = 300 + i * 100;
this.m[i] = 0;
}
// 主循环
var timer:egret.Timer = new egret.Timer(30);
timer.addEventListener(
egret.TimerEvent.
TIMER,
this.update,
this);
timer.start();
this.touchEnabled = true;
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,
this.touchDown,
this,true
);
this.touchEnabled = true;
this.addEventListener(egret.TouchEvent.TOUCH_MOVE,
this.touchMove,
this,true
);
this.touchEnabled = true;
this.addEventListener(egret.TouchEvent.TOUCH_END,
this.touchUp,
this,true
);
}
public moveCar(tx : number ,ty : number){
if(Math.abs(tx - this.fox.x)<70 && Math.abs(ty - this.fox.y)<70){
this.fox.x = tx;
this.fox.y = ty - 50;
}
}
public touchDown(e:egret.TouchEvent){
this.moveCar(e.stageX,e.$stageY);
}
public touchMove(e:egret.TouchEvent){
this.moveCar(e.stageX,e.$stageY);
}
public touchUp(e:egret.TouchEvent){
this.moveCar(e.stageX,e.$stageY);
}
public update() {
for(var i:number=0;i<5;i++){
switch(this.m[i]){
case 0:
this.car[i].x -= 5 ;
if(this.car[i].x < 100){
this.m[i] = 1;
this.car[i].scaleX = -1;
}
break;
case 1:
this.car[i].x += 5 ;
if(this.car[i].x > 550){
this.m[i] = 0;
this.car[i].scaleX = 1;
}
break;
}
// 玩家死亡,游戏归零,重新开始
if(Math.abs(this.fox.x - this.car[i].x) < 50 && Math.abs(this.fox.y - this.car[i].y) <50 ){
this.fox.x = 300;
this.fox.y = 1000;
this.lose ++ ;
}
}
// 玩家胜利,重新回到起点。
if(this.fox.y < 100){
this.fox.x = 300;
this.fox.y = 1000;
this.win ++ ;
}
this.tj.text = "胜利:" + this.win + "失败:" + this.lose ;
}
/**
* 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
* Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
*/
private createBitmapByName(name: string): egret.Bitmap {
let result = new egret.Bitmap();
let texture: egret.Texture = RES.getRes(name);
result.texture = texture;
return result;
}
/**
* 描述文件加载成功,开始播放动画
* Description file loading is successful, start to play the animation
*/
private startAnimation(result: Array<any>): void {
let parser = new egret.HtmlTextParser();
let textflowArr = result.map(text => parser.parse(text));
let textfield = this.textfield;
let count = -1;
let change = () => {
count++;
if (count >= textflowArr.length) {
count = 0;
}
let textFlow = textflowArr[count];
// 切换描述内容
// Switch to described content
textfield.textFlow = textFlow;
let tw = egret.Tween.get(textfield);
tw.to({ "alpha": 1 }, 200);
tw.wait(2000);
tw.to({ "alpha": 0 }, 200);
tw.call(change, this);
};
change();
}
/**
* 点击按钮
* Click the button
*/
private onButtonClick(e: egret.TouchEvent) {
let panel = new eui.Panel();
panel.title = "Title";
panel.horizontalCenter = 0;
panel.verticalCenter = 0;
this.addChild(panel);
}
}