如何扩展内置组件?

我想扩展内置Label组件,为其添加阴影:

cc.Class({
  name: 'LabelEx',
  extends: cc.Label,
  properties: {
    shadowColor: {
      type: cc.Color,
      default: cc.Color.BLACK
    },
    shadowOffset: {
      type: cc.Vec2,
      default: cc.Vec2.ZERO
    }
  },
  ctor() {
    this._shadowNode = null
  },
  onLoad() {
    if (this._shadowNode !== null) return;
    this._shadowNode = cc.instantiate(this.node);
    this._shadowNode.color = this.shadowColor;
    this._shadowNode.setPosition(cc.v2(this.node.x + this.shadowOffset.x, this.node.y + this.shadowOffset.y));
    this.node.parent.addChild(this._shadowNode, this._shadowNode.zIndex - 1);
  }
});

但是为sprite添加上LabelEx之后CocosCreator报错然后就卡住了。
请问如何正确扩展cocos内置组件?

错误不知道,但是可以推断 你的 cc.instantiate(this.node) 有问题,如果你克隆的是本节点,那么节点上的LabelEx组件 也会被克隆,克隆出来的组件还会继续克隆。
这样的结果会造成调用的死循环。

发现最新引擎已经有CCLabelShadow组件了:joy: