Hello,
I am using Cocos Creator 2.0.2 on Windows 7.
I want to programmatically create a new node with a label and a sprite components.
Here is what I am doing
class MyChildNodeType extends cc.Node {
private titleLabel: cc.Label;
private backgroundSprite: cc.Sprite;
constructor(text, spriteFrame) {
super();
this.width = 100;
this.height = 100;
this.color = cc.Color.BLACK;
this.titleLabel = this.addComponent(cc.Label);
this.titleLabel.string = text;
this.backgroundSprite = this.addComponent(cc.Sprite);
this.backgroundSprite.spriteFrame = spriteFrame;
}
onLoad() {
console.log("load activity block!");
}
update() {
console.log("update activity block!");
}
}
class MyParentNodeType extends cc.Node {
//...
rebuildChildren() {
const myNewNode = new MyChildNodeType("hey", mySpriteFrame);
myNewNode.setPosition(0, 0);
this.node.addChild(myNewNode, this.node.zIndex + 10000);
}
}
When debugging, the node seem to have been instanciated, and the values inside it look ok.
However, when running it, I see nothing.
It never breaks on breakpoints at myNewNode.onLoad()
and myNewNode.update()
. So I think we can safely assume that this node is never loaded or updated.
Is there something I missed?
I’ve seen that there are both a parentNode.addChild(childNode)
and a childNode.setParent(parentNode)
methods. Using both at the same time fails with an “Node already added!” error. So I believe they do the same thing?
Thank you for any help