学习Egret小白入门篇
2018-01-07 本文已影响101人
彬少灬
根据官方小白入门文档一步一步抄录测试
Main.ts 文件
class Main extends eui.UILayer {
private webSocket:egret.WebSocket;
private craateGameScene():void{
this.webSocket = new egret.WebSocket();
this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen,this);
this.webSocket.connect("echo.websocket.org",80);
}
private onSocketOpen():void {
var cmd = "Hello Egret WebSocket";
console.log("The connection is successful, send data: " + cmd);
this.webSocket.writeUTF(cmd);
}
private onReceiveMessage(e:egret.Event):void {
var msg = this.webSocket.readUTF();
console.log("Receive data:" + msg);
}
private times:number;
// //添加了文字touch事件
// private touchHandler( evt:egret.TouchEvent ):void{
// var tx:egret.TextField = evt.currentTarget;
// tx.textColor = 0x00ff00;
// tx.size = 60;
// }
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());
this.runGame().catch(e => {
console.log(e);
})
}
private async runGame() {
await this.loadResource()
this.createGameScene();
const result = await RES.getResAsync("description_json")
//this.startAnimation(result);
await platform.login();
const userInfo = await platform.getUserInfo();
console.log(userInfo);
}
private async loadResource() {
try {
const loadingView = new LoadingUI();
this.stage.addChild(loadingView);
await RES.loadConfig("resource/default.res.json", "resource/");
await this.loadTheme();
await RES.loadGroup("heroes", 0, loadingView);
this.stage.removeChild(loadingView);
}
catch (e) {
console.error(e);
}
}
private loadTheme() {
return new Promise((resolve, reject) => {
// 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, () => {
resolve();
}, this);
})
}
private textfield: egret.TextField;
/**
* 创建场景界面
* Create scene interface
*/
// ----------------------------------------------以下是编写命令部分---------------------------------------------------------
protected createGameScene(): void {
//实例化egret.Shape()图形绘制的类
var bg:egret.Shape = new egret.Shape();
bg.graphics.beginFill(0x0000ff);
bg.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
bg.graphics.endFill();
this.addChild(bg);
// var tx:egret.TextField = new egret.TextField();
// tx.text = "Hellow World I'm shenyubin";
// tx.size = 30;
// tx.x = 50;
// tx.y = 50;
// tx.width = this.stage.stageWidth - 100;
// tx.touchEnabled = true;
// // tx.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
// tx.addEventListener(egret.TouchEvent.TOUCH_TAP,function(evt:egret.TouchEvent):void{
// tx.textColor = 0x00ff00;
// },this);
// this.addChild(tx);
// console.log("createGameScene",RES.getRes("hero-01"));
var superman:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-01_jpg"));
superman.x = 20;
superman.y = 20;
this.addChild(superman);
var batman:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-03_jpg"));
batman.x = 120;
batman.y = 20;
this.addChild(batman);
var hulk:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-02_jpg"));
hulk.x = 220;
hulk.y = 20;
this.addChild(hulk);
var captain:egret.Bitmap = new egret.Bitmap(RES.getRes("hero-04_jpg"));
captain.x = 320;
captain.y = 20;
this.addChild(captain);
this.setChildIndex( batman,this.getChildIndex(hulk));
//console.log( "display indexes:", this.getChildIndex( bg ), this.getChildIndex( superman ), this.getChildIndex( batman ), this.getChildIndex( hulk ), this.getChildIndex( captain ) );
hulk.anchorOffsetX = 30;
hulk.anchorOffsetY = 40;
hulk.x += 30;
hulk.y += 40;
this.times = -1;
var self = this;
this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
switch( ++ self.times % 3){
case 0:
//batman and superman 互换位置
egret.Tween.get(batman).to({x:superman.x},300,egret.Ease.circIn);
egret.Tween.get(superman).to({x:batman.x},300,egret.Ease.circIn);
break;
case 1:
//superman透明度变化
egret.Tween.get(captain).to({alpha:.4},300,egret.Ease.circIn).to({alpha:1},300,egret.Ease.circIn);
break;
case 2:
//hulk大小变化
egret.Tween.get(hulk).to({scaleX:.4,scaleY:.4},600,egret.Ease.circIn).to({scaleX:1,scaleY:1},600,egret.Ease.circIn);
break;
}
},this);
//加入音乐
var sound:egret.Sound = RES.getRes("bonus_mp3");
var channel:egret.SoundChannel = sound.play(0,1);
//网络通讯
var urlreq:egret.URLRequest = new egret.URLRequest("http://httpbin.org/user-agent");
var urlloader:egret.URLLoader = new egret.URLLoader();
urlloader.addEventListener(egret.Event.COMPLETE,function(evt:egret.Event):void{
console.log(evt.target.data);
},this);
urlloader.load(urlreq);
}
}