Child already added. It can't be added again

The code at the end of this post is throwing the error “child already added. It can’t be added again” all the time.

From the stack, it seems the problem arises on this line:

var menuItemPlay = new cc.MenuItemLabel("Play", this.onPlay, this);

I am creating such label there and, obviously, I haven’t added it myself nowhere before that code.

Any ideas? Thanks a lot in advance.

var MenuLayer = cc.Layer.extend({
	ctor : function() {
		this._super();
	},
	init : function() {
		this._super();
		var winsize = cc.director.getWinSize();
		var centerpos = cc.p(winsize.width / 2, winsize.height / 2);
		var spritebg = new cc.Sprite(res.general_bkg);
		spritebg.setPosition(centerpos);
		this.addChild(spritebg);
		cc.MenuItemFont.setFontSize(60);
		var menuItemPlay = new cc.MenuItemLabel("Play", this.onPlay, this);
		var menu = new cc.Menu(menuItemPlay);
		menu.setPosition(centerpos);
		this.addChild(menu);
	},

	onPlay : function() {
		cc.log("==onplay clicked");
	}
});

It’s so strange that this popular problem (that I’ve faced myself too) has not been solved. Please @pandamicro answer this !

I have a similar code like this and the simulator has thrown me an exact error like the author:

.....
onLoad: function(){
   var url = cc.url.raw("resources/map_wall/gray_h.png");
   var texture = cc.textureCache.addImage(url);
   var wall_h = cc.instantiate(new cc.Sprite(new cc.SpriteFrame(texture)));
   this.node.addChild(wall_h);
},
......

It threw me “Child already added. It can’t be added again” !

1 Like

new cc.Menu(menuItemPlay) is adding menuItemPlay as a child of menu, are you adding menuItemPlay to other place ?

You can also add a break point to the error log: child already added. It can't be added again, then take a look at the type of the child and the parent to figure out where is triggering the issue exactly. Or adding log everywhere to narrow down to the exact line of code which trigger the problem. I don’t think new cc.MenuItemLabel can cause the issue.

@gadapchetvoi

As for you, I think you are using Creator, cc.instantiate will copy the parent property from the original object too, so you need to do the following:

onLoad: function(){
   var url = cc.url.raw("resources/map_wall/gray_h.png");
   var texture = cc.textureCache.addImage(url);
   var wall_h = cc.instantiate(new cc.Sprite(new cc.SpriteFrame(texture)));
   wall_h.parent = null;
   this.node.addChild(wall_h);
},
2 Likes

Thank you @pandamicro
I didn’t think that creating a “temporary” sprite node (which is not child of anything) still make it has a parent.

Actually instantiate is simply deserializing the original data, so parent is also copied, maybe it could be improved. @jare what do you think

Sorry for the inconvenient.Here you should not instantiate cc.Component, please instantiate cc.Node to avoid this error.

Hi there,
I Am using
const texture = cc.textureCache.addImage(getResourceURL(url));
cc.SpriteFrame.create(texture, rect);
this.addChild(this.sprite);

with above i am also facing same issue can you help me out .