cocos creator 3 踩坑

预制资源(Prefab)无法显示

原因

实例化 prefab 为节点之后,添加到的父节点的 LayerUI_2D,但是 prefab 的 Layer 是默认的

解决

修改 prefab 的 LayerUI_2D 即可

1

组件的 start 方法在节点进入舞台时调用

例如:

1
2
3
4
// 实例化 prefab 为 node
let tempNode = instantiate(this.tempPrefab);
// 然后把 node 加入到舞台中,这时候,会调用 节点挂载的组件的 start 方法
this.parentNode.addChild(tempNode);

bind 的使用

怎样在回调函数中获取当前组件的属性。

使用情况ref:https://docs.cocos.com/creator/manual/zh/engine/event/event-emit.html#%E7%9B%91%E5%90%AC%E4%BA%8B%E4%BB%B6

以下两种调用方式,效果上是相同。

1
2
3
4
5
6
7
8
9
// 使用函数绑定
eventTarget.on('foo', function ( event ) {
this.enabled = false;
}.bind(this));

// 使用第三个参数
eventTarget.on('foo', (event) => {
this.enabled = false;
}, this);


cocos creator 3 踩坑
https://hutaoren.cn/2022/10/24/cocos creator 3 踩坑/
作者
胡桃仁
发布于
2022年10月24日
许可协议