LayaBox的场景切换

class GamOver extends ui.GameOverUI{
    constructor(){
         super();
         this.restart_btn.on(Laya.Event.CLICK,this,this.restartGame);
    }
    
    //点击重新开始按钮触发下面的事件
    public restartGame():void{
        this.removeSelf();              //移除游戏结束界面
        GameMain.GameView.removeSelf(); //移除游戏主体
        Laya.stage.addChild(GameMain.GameStart); //添加游戏开始界面
    }
    
    //添加分数或者排名到页面上去
    public addScore(score:number):void{
        var textScore:Laya.Text = new Laya.Text();
        textScore.text = String(score);  //或者是 score.toString();  ts的强制类型转换
        //textScore.x = 280;
        textScore.align = "center";
        textScore.width = 640;
        textScore.height = 800;
        textScore.valign = "middle";
        textScore.fontSize = 80;
        textScore.color = "red";
        Laya.stage.addChild(textScore);
    }
    
}

例如这里的例子,这里是在游戏结束的 时候点击重新开始会执行restartGame()的方法,里面就用removeSelf()的方法来移除当前的类GamOver的UI类,就意味着游戏结束的界面被移除了,不再显示了,然后再下面的Laya.stage.addChild()方法里面又添加了GameStart的UI对象,就显示游戏开始的界面了,还可以通过下面的方法销毁或者移除当前UI类,从而达到场景切换的功能(其实也是对节点的操作):

原文地址:https://www.cnblogs.com/Alisa-Davillver/p/6165234.html