// 程序入口 class GameMain{ public static GameView:GamView; //用一個公開的靜態屬性來保存GameView頁面,方便全局調用,控制其銷毀或者添加到畫布 public static GameStart:GamStart; //用一個公開的靜態屬性來保存GameStart頁面,方便全局調用,控制其銷毀或者添加到畫布 public static GameOver:GamOver; //用一個公開的靜態屬性來保存GameOver頁面,方便全局調用,控制其銷毀或者添加到畫布 private progressBar:Laya.ProgressBar; //進度條屬性 constructor() { Laya.init(640,960); Laya.stage.scaleMode = Laya.Stage.SCALE_FIXED_WIDTH; // Laya.stage.screenMode = Laya.Stage.SCREEN_NONE; //Laya.stage.alignH = Laya.Stage.ALIGN_CENTER; //Laya.stage.alignV = Laya.Stage.ALIGN_MIDDLE; Laya.stage.bgColor = "#7a654f"; //開始加載進度條 //進度條資源加載 var pro_res:Array<any> = [ {url:"res/atlas/progress_time$bar.png",type:Laya.Loader.IMAGE}, //進度條的圖片資源位置和類型 {url:"res/atlas/progress_time.png",type:Laya.Loader.IMAGE} //進度條的圖片資源位置和類型 ];
//加載完進度條后執行onProLoaded方法 Laya.loader.load(pro_res,Laya.Handler.create(this,this.onProLoaded)); //加載圖片 // Laya.loader.load(res,Laya.Handler.create(this,this.onLoad)); //加載完以后會有一個回調函數 } //加載進度條圖片資源的同時,回調函數觸發下面的方法加載主游戲資源 public onProLoaded():void{ //顯示進度條圖片 this.progressShow(); //預加載主游戲頁面圖片資源數組 var res:Array<any> = [ {url:"res/atlas/ui.json",type:Laya.Loader.ATLAS}, {url:"res/atlas/ui.png",type:Laya.Loader.IMAGE}, {url:"res/atlas/bg.mp3",type:Laya.Loader.SOUND}, {url:"res/atlas/hit.wav",type:Laya.Loader.SOUND} ]; //設置progress Handler的第4個參數為true,根據加載文件個數獲取加載進度 Laya.loader.load(res,null,Laya.Handler.create(this,this.onProgress,null,false)); } //顯示開始游戲加載進度條 public progressShow():void{ //和text一樣,需要先new一個進度條對象 this.progressBar = new Laya.ProgressBar("res/atlas/progress_time.png"); this.progressBar.width = 400; this.progressBar.pos(130,500); this.progressBar.sizeGrid = "5,5,5,5"; //當進度條發生變化的時候,我們需要下面的方法來監聽其變化 this.progressBar.changeHandler = new Laya.Handler(this,this.onChange); //添加進度條到舞台上 Laya.stage.addChild(this.progressBar); } //主游戲界面加載完成后的回調函數 public onProgress(pro:number):void{ //console.log("加載了總文件的:"+pro*100+"%"); this.progressBar.value=pro; if(this.progressBar.value==1) { //游戲主頁面資源加載完成后執行這里的代碼 //console.log("游戲加載完成咯!!"); //延遲1秒再顯示游戲主頁面 this.progressBar.value=pro; Laya.timer.once(1000,this,this.onLoad); //this.progressBar.visible = false; // laya.media.SoundManager.playMusic("res/atlas/bg.mp3",0); } } //進度條發生變化的時候觸發下面的方法 public onChange(value:number):void{ console.log("進度: "+Math.floor(value*100)+"%"); } //加載完成后的回調函數 public onLoad():void{ laya.media.SoundManager.playMusic("res/atlas/bg.mp3",0); //移除進度條 Laya.stage.removeChild(this.progressBar); // 實例化游戲開始界面 GameMain.GameStart = new GamStart(); //注意哦,這里的GameStart是靜態屬性,所以訪問的時候不能用this了,只能用GameMain類, Laya.stage.addChild(GameMain.GameStart); } } new GameMain();