egret场景路由管理类

关注微信公众号:每日玩机 获取ios、Android、tv、mac黑科技软件

在这里插入图片描述
新建ts文件写入SceneManager工具类,具体如下:

class SceneManager {
    private static _manager: SceneManager;
    public static get Instance() {
        if (SceneManager._manager == null) {
            SceneManager._manager = new SceneManager();
        }
        return SceneManager._manager;
    }
    public constructor() {
    }

    public rootLayer: eui.UILayer;//起始场景
    private currentScene: Scene;//需要显示的场景
    private pop_scene: Scene;//弹出场景层
    //切换场景
    public changeScene(s: Scene) {
        if (this.currentScene) {
            this.rootLayer.removeChild(this.currentScene);
            this.currentScene = null;
        }
        this.popScene();
        this.rootLayer.addChild(s);
        this.currentScene = s;
    }

    //弹出场景层 
    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

  1. 新建一个Scene.ts文件
abstract class Scene extends eui.Component{
    public constructor() {
        super();
        // 监听组件创建完毕 也就是场景的外观创建完毕
        this.addEventListener(eui.UIEvent.CREATION_COMPLETE,this.onComplete,this);
    }
     protected abstract onComplete();
}
  1. 在以后新建的eui组件中ts文件就得继承该Scene,具体改成如下样式,只有是继承Scene的页面才可以使用该工具类进行管理
class page extends Scene {
	public constructor() {
		super();
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}

	protected childrenCreated(): void {
		super.childrenCreated();
	}
	protected onComplete(): void {

	}
}

具体使用如下

  1. 第一步也是很重要的一步,初始化第一个场景

调用工具类的rootLayer 设置初始页面
注:
因为我是设置的loading页面为初始页,而loading页时extends的eui.UILayer,所以我在SceneManager 类中设置的rootLayer 也为eui.UILayer,只有这两个类型相同才能初始化成功,具体类型根据自己的需要去设置

SceneManager.Instance.rootLayer = this;
  1. 切换页面并删除当前页面
var homePage = new home_page();
SceneManager.Instance.changeScene(homePage);
  1. 弹出页面,不删除当前页面
var homePage = new home_page();
SceneManager.Instance.pushScene(homePage);
  1. 删除当前页面
SceneManager.Instance.popScene();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值