cocos creator从零开发简单框架(03)-框架初始化

新建framework/scripts/App.ts,用于初始化框架,内容如下。因为Cocos Creator 2.x版本不能直接在编辑器运行,导致不能实时看到运行中的对象节点信息,所以这里写了个showNode函数用来打印Canvas节点信息。

import LayerMgr from "./view/LayerMgr"


export default class App {
    // 初始化
    public static init() {
        this.initFramework()

        this.showNode(cc.Canvas.instance.node)
    }


    // 递归显示节点信息
    public static showNode(node: cc.Node, sep: string = '', depth: number = 0, depthMax: number = 4) {
        console.log(sep + node.name + ':' + node.zIndex + ':' + node.active)
        if (node.childrenCount <= 0) return

        depth++
        if (depth >= depthMax) return

        node.children.forEach(child => this.showNode(child, sep + '\t', depth, depthMax))
    }


    // 初始化框架
    private static initFramework() {
        LayerMgr.init()
    }
}

新建scripts/Main.ts,用于启动程序,内容如下。

import App from "../framework/scripts/App"

const { ccclass } = cc._decorator


@ccclass
export default class Main extends cc.Component {
    protected onLoad(): void {
        App.init()
    }
}

Main.ts节本挂载到场景Canvas节点上,然后运行程序,可以看到UIPanelTop这 3 个层级已经创建了。

挂载Main脚本

运行游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值