cocos creator从零开发2048(11)-游戏失败处理

Canvas 节点下新建空节点并重命名为 panelFailed,挂载 Widget 组件并勾选 TopRightBottomLeft 并把这些属性都设置为 0,挂载 BlockInputEvents 组件。

panelFailed节点

panelFailed 节点下新建 Sprite (单色) 并重命名为 bgSize 设置为 500x500Color 设置为#43C1BE

panelFailed 节点下新建 Label (文字)Position Y 设置为 200String 输入游戏失败了,再来一次吧!

panelFailed 节点下新建 Button (按钮) 并重命名为 btnOkbtnOk/Background/Label 属性 String 输入好的

ui完成

编辑 scripts/Game.ts,添加 initPanelFailed 方法初始化失败面板。

private _panelFailed: cc.Node
private initPanelFailed() {
    this._panelFailed = this.node.getChildByName('panelFailed')
    this._panelFailed.active = false

    this._panelFailed.getChildByName('btnOk').on('click', () => cc.game.restart())
}

修改 init 方法,调用 initPanelFailed 方法。

btnRestart.on('click', () => cc.game.restart())

this.initPanelFailed()

添加 checkFail 方法检查游戏是否失败。

private checkFail() {
    const gridList = [this.grids, this.gridsReversed]
    for (const grids of gridList) {
        for (const items of grids) {
            for (let i = 1; i < items.length; i++) {
                if (!items[i].num || !items[i - 1].num || items[i].num == items[i - 1].num) return
            }
        }
    }

    this._moving = true
    this.scheduleOnce(() => this._panelFailed.active = true, 1)
}

修改 onMoveAfter 方法,调用 checkFail 方法。

this.randGrid()
this.checkFail()
本作品采用《CC 协议》,转载必须注明作者和本文链接