cocos creator从零开发简单框架(23)-Panel事件处理

抖音小游戏上线有倒计时类的程序,如果弹出了窗口,要求暂停倒计时,窗口关闭后继续倒计时。所以Panel管理器需要抛出窗口显示隐藏这两个事件。

编辑framework/scripts/AppConstants.ts,添加PanelShowPanelHide定义。

// Panel 遮罩类型
public static readonly panelMaskStyle = {
    // 不可穿透
    NoThrough: 0b1,
    // 半透明
    Black: 0b10,
    // 关闭组件
    Close: 0b100,
}

// 事件
public static readonly event = {
    PanelShow: 'PanelShow',
    PanelHide: 'PanelHide',
}

编辑framework/scripts/manager/EventMgr.ts,修改emit方法过滤PanelShowPanelHide未注册事件日志输出(因为有的平台不需要监听这两个事件)。

public static emit(event: string, ...params: any[]) {
    if (!this._events.has(event)) {
        if (event != AppConstants.event.PanelShow && event != AppConstants.event.PanelHide) {
            console.error('EventMgr.emit event:' + event + ' 未注册')
        }
        return
    }

    this._events.get(event).forEach(({ cb, ctx }) => {
        cb.apply(ctx, params)
    })
}

编辑framework/scripts/view/PanelMgr.ts,修改hideAll方法。

public static hideAll() {
    this._panels.forEach(panel => {
        if (panel.skin.active) {
            this.destroy(panel.panelName)
        }
    })

    EventMgr.emit(AppConstants.event.PanelHide)
}

修改showPanelAfter2方法。

private static showPanelAfter2(isOpen: boolean) {
    TopBlock.hide()

    if (isOpen) {
        EventMgr.emit(AppConstants.event.PanelShow)
    } else {
        let hide = true
        this._panels.forEach(panel => {
            if (panel.skin.active) hide = false
        })
        hide && EventMgr.emit(AppConstants.event.PanelHide)
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!