egret入門單例模式場景切換講解
一:簡介
小弟近日接觸egret遊戲引擎,只要是爲了自己熟練運用和鞏固複習,希望能爲入門小白引路。
小弟也不大熟悉egret遊戲引擎,如有不到位之處請大牛指教。
小弟只講解最最最基本的內容,詳情請根據實際情況。
二:egret場景切換講解
小弟並沒用在egret中找到相關跳轉場景的API,所以引用網上查找的單例模式場景切換;
熟悉的請點擊這裏
小弟在此中刪除了一些自認爲不重要的代碼,如有不滿請不要對小弟發泄!
哈哈哈哈哈哈哈!!!
abstract class Scene extends eui.Component{
public constructor() {
super();
}
}
absstract將這個類設置爲虛類,在子類中實現繼承。
class SceneManager {
private static _manager:SceneManager;
public static get Instance(){
if( SceneManager._manager==null){
SceneManager._manager = newSceneManager();
}
return SceneManager._manager;
}
public constructor() {
}
}
創建場景管理類,實現單例模式,保證場景管理類只有一個實例。
public rootLayer:eui.UILayer;//起始場景
private currentScene:Scene;//需要顯示的場景
private pop_scene:Scene;//彈出場景層
這裏請注意rootLayer應用,請在Main.ts的createGameScene()中:
SceneManager.Instance.rootLayer = this;
切換場景方法的寫法如下,在rootLayer上進行移除和添加:
//切換場景
public changeScene(s:Scene){
if(this.currentScene){
this.rootLayer.removeChild(this.currentScene);
this.currentScene = null;
}
this.rootLayer.addChild(s);
this.currentScene = s;
}
彈出與關閉場景層與上類似是,在rootLayer上進行移除和添加:
//彈出場景層
public pushScene(s:Scene){
this.popScene();
if(!this.pop_scene){
this.rootLayer.addChild(s);
this.pop_scene = s;
}
}
//關閉場景層
public popScene(){
if(this.pop_scene){
this.rootLayer.removeChild(this.pop_scene);
this.pop_scene = null;
}
}
其他添加eui等相關操作請繼承Scene類,實現其相關方法即可。
在eui類中使用下列方法:
切換場景方法:
let s1:StartScene = new StartScene();
SceneManager.Instance.changeScene(s1);
彈出場景層方法
在對應的點擊事件中調用pushScene()方法。
let tc:Tanchu = new Tanchu();
SceneManager.Instance.pushScene(tc);//添加場景彈出層
如果需要關閉彈出場景層,在彈出場景層的類中調用popScene()方法。
SceneManager.Instance.popScene();//移除場景層
總結告白
睡覺了,“放下屠刀,一心向邪”,請好好努力,擁有更美好的未來。
如果!!!v!!!有一天!!!w!!!我變的很有錢~~~~~~~~~~~~