創(chuàng)建類
class MyScene extends egret.Sprite {
public constructor() {
super();
MyScene.SEX = "男";
this.addEventListener(egret.Event.ADDED_TO_STAGE,function(){
// 當(dāng)當(dāng)前容器添加到舞臺后再執(zhí)行
},this)
}
public name;//公開屬性
private age;//私有屬性,不允許在實例化對象中調(diào)用,只可以在myScene類的內(nèi)部使用
}
封裝的方法
//繪制圖片容器
function createImg(imgName){
let myImg = new egret.Bitmap();
myImg.texture = RES.getRes(imgName);
return myImg;
}
、、繪制文本域
function createText(text,size=30,color=0x000000,textAlign="center",verticalAlign="MIDDLE"){
let myText = new egret.TextField();
myText.text = text;
myText.size = size;
myText.textColor = color;
myText.textAlign = textAlign;
myText.verticalAlign = verticalAlign;
return myText;
}
//繪制矩形
function createRect(width:number,height:number,bgColor=0x000000,alpha=1,lineWidth=0,lineColor=0x000000){
let myRect = new egret.Shape();
myRect.graphics.beginFill(bgColor,alpha);
myRect.graphics.lineStyle(lineWidth,lineColor);
myRect.graphics.drawRect(0,0,width,height);
myRect.graphics.endFill();
return myRect;
}
//繪制按鈕
function createBtn(text,width,height,callback=function(){},bgColor=0xffff00,hoverBgColor=0xff4500,textSize=60,textColor=0xffffff,hoverTextColor=0x234567){
let myBtn = new egret.Sprite();
// 按鈕里面的文字
let myText = new egret.TextField();
myText.text = text;
myText.width = width;
myText.height = height;
myText.textAlign = egret.HorizontalAlign.CENTER;
myText.verticalAlign = egret.VerticalAlign.MIDDLE;
myText.textColor = textColor;
myText.size = textSize;
// 繪制按鈕的矩形
myBtn.graphics.beginFill(bgColor);
myBtn.graphics.drawRect(0,0,width,height);
myBtn.graphics.endFill();
myBtn.addChild(myText);
// 添加點擊事件
myBtn.touchEnabled = true;
myBtn.addEventListener(egret.TouchEvent.TOUCH_BEGIN,function(){
myBtn.graphics.clear();
myBtn.graphics.beginFill(hoverBgColor);
myBtn.graphics.drawRect(0,0,width,height);
myBtn.graphics.endFill();
myText.textColor = hoverTextColor;
},null)
// 執(zhí)行按鈕的點擊事件
myBtn.addEventListener(egret.TouchEvent.TOUCH_END,function(){
myBtn.graphics.clear();
myBtn.graphics.beginFill(bgColor);
myBtn.graphics.drawRect(0,0,width,height);
myBtn.graphics.endFill();
myText.textColor = textColor;
callback();
},null)
return myBtn;
}
// 繪制一個圓角矩形
function createRoundRect(width:number,height:number,eclipW,eclipH){
let myRoundRect = new egret.Shape();
myRoundRect.graphics.lineStyle(8,0xffff00);
myRoundRect.graphics.drawRoundRect(0,0,width,height,eclipW,eclipH);
return myRoundRect;
}
自定義進(jìn)度條事例
//進(jìn)度條部分
class progressBar extends egret.Sprite{
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE,function(){
this.pW = this.stage.stageWidth*.8;
this.drawBorder();
},this);
}
public pW:number;
public pH:number = 40;
// 畫出邊框
public drawBorder(){
this.graphics.clear();
this.graphics.beginFill(0xffffff,0);
this.graphics.lineStyle(4,0x00ff00);
this.graphics.drawRoundRect(0,0,this.pW,this.pH,10,10);
this.graphics.endFill();
this.anchorOffsetX = this.pW*.5;
this.anchorOffsetY = this.pH*.5;
}
private bgR:egret.Shape = new egret.Shape();
// 畫出進(jìn)度條
public drawBg(width){
width = width>=this.pW-4?this.pW-4:width;
this.bgR.graphics.clear();
this.bgR.graphics.beginFill(0xeb6616);
this.bgR.graphics.drawRect(2,2,width,this.pH-4);
this.bgR.graphics.endFill();
this.addChild(this.bgR);
}
}
//LoadingUI
class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE,function(){
this.proB = new progressBar();
this.proB.x = this.stage.stageWidth*.5;
this.proB.y = this.stage.stageHeight*.5;
this.addChild(this.proB);
},this)
}
private proB:progressBar;
public onProgress(current: number, total: number): void {
let curW:number = this.proB.pW*current/total;
this.proB.drawBg(curW);
}
}
加載資源的方法
//同步獲取資源,這種方式只能獲取緩存過的資源
RES.getRES(name:string)
//異步獲取資源,這種方式可以獲取配置中的所有資源項。如果緩存中存在則直接調(diào)用回調(diào)函數(shù)
RES.getRESAsync(name:string,compFunc:function,thisObject)
//通過url獲取不在配置中的資源,通常不建議使用這個接口,只在獲取其他服務(wù)器的資源時使用
RES.getResByUrl(url:string,compFunc:function,thisObject,type:string = "")
加載資源組
private async loadResource(){//加載資源組
//實例化進(jìn)度條
let loadingView = new LoadingUI();
this.addChild(loadingView);
//加載資源組,第一個參數(shù)為資源組名稱,第二個參數(shù)為優(yōu)先級,第三個參數(shù)為loadingUI
await RES.loadGroup("player",0,loadingView);
//加載完成后移除進(jìn)度條
this.removeChild(loadingView);
this.createGameScene();
}
播放音樂
//獲取音樂sound對象
let sound = RES.getRes("music");
//通過sound.play創(chuàng)建soundChannel對象,用來控制聲音播放
soundC = sound.play();
計時器的使用
Timer
//第一個參數(shù)為播放的間隔時間,第二個參數(shù)為執(zhí)行次數(shù),若為0則重復(fù)執(zhí)行
let timer = new egret.Timer(100,1);
//每次到達(dá)間隔時間時調(diào)用
timer.addEventListener(egret.TimerEvent.TIMER,function(){
console.log("timer");
},this);
//當(dāng)計時器到達(dá)重復(fù)次數(shù)后調(diào)用
timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,function(){
console.log("timer執(zhí)行完畢");
},this)
ticker
//startTick全局函數(shù)將以 60 幀速率回調(diào)函數(shù)
private function tickerC(){
console.log("我是ticker");
return false;
}
//第一個參數(shù)為回調(diào)函數(shù),第二個參數(shù)為this的指向
egret.startTick(this.tickerC,this);
egret.stopTick(this.tickerC,this);
幀事件
//幀事件的回調(diào)與幀率的相同
this.addEventListener(egret.Event.ENTER_FRAME,function(){
console.log("我是幀事件");
},this)
場景管理類事例
class SceneManager {
//通過單例模式生產(chǎn)管理類
static obj:SceneManager;
static createObj(_stage){
if(!SceneManager.obj){
SceneManager.obj = new SceneManager(_stage);
}
return SceneManager.obj;
}
/*
1、能夠幫我們快速加載資源
2、能夠幫我們快速切換場景
*/
public constructor(_stage) {
this._stage = _stage;
}
private _stage;
private curScene;
public async loadScene(sceneName,NextScene){
// 功能一:加載資源
let loadingView = new LoadingUI();
this._stage.addChild(loadingView);
await RES.loadGroup(sceneName,0,loadingView);
this._stage.removeChild(loadingView);
// 功能二:切換場景
let nextScene = new NextScene();
this._stage.addChild(nextScene);
nextScene.x = this._stage.stage.stageWidth;
egret.Tween.get(nextScene).to({x:0},400,egret.Ease.backInOut).call(()=>{
if(this.curScene && this._stage.contains(this.curScene)){
this._stage.removeChild(this.curScene);
}
this.curScene = nextScene
})
}
}
// 調(diào)用場景管理類來加載場景
SceneManager.createObj(this.stage).loadScene("welcome",welcome);
創(chuàng)建幀動畫
//第一步:實例化一個movieClipData的工廠
//第一個參數(shù)為MovieClip數(shù)據(jù)集,該數(shù)據(jù)集必須由Egret官方工具生成,第二個參數(shù)為紋理
let mcDataFactory = new egret.MovieClipDataFactory(RES.getRes("default_json"),RES.getRes("default_png"));
// 第二步:通過mcDataFactory得到movieClipData,接收一個動作參數(shù),由json文件獲取到
let mcData = mcDataFactory.generateMovieClipData("move");
// 第三步:實例化出來一個movieClip
let mc = new egret.MovieClip(mcData);
this.addChild(mc);
// 第四步,播放mc動畫
//可傳入number,<1為無限播放,大于1則指定播放次數(shù)
mc.play(-1);
//跳到指定幀播放
mc.gotoAndPlay("move2",-1);