user2820692
user2820692

Reputation: 33

How to remove child layer cocos2d

I have small problem with removing a child Layer. I am not sure how to remove the layer correctly.

Here is my code sample

-(void)GoMoveFirst
{
    //
    //====HERE IS COMBOBOX START

    CCMenuItemImage *lvl1 = newButton(@"retry1", 200, 590, self, @selector(onRetry));
    CCMenuItemImage *lvl2 = newButton(@"retry1", 500, 590, self, @selector(onRetry)); //onHighScore:
    CCMenuItemImage *lvl3 = newButton(@"retry1", 800, 590, self, @selector(onRetry));
    CCMenu *menu = [CCMenu menuWithItems:lvl1, lvl2,lvl3, nil];
    menu.position = ccp(0, 0);
    [GameLayer addChild:menu z:103];
}

After I press one of this buttons, I need it to be removed. All stuff is going here:

-(void)onRetry
{
    //
//HERE i need remove menu Child.
//

    [m_sGo runAction:[CCSequence actions:actionMove, [CCCallFunc actionWithTarget:self selector:@selector(GoMoveSecond)], nil]];
}

One idea was try make it invisible, but I do not think this would be a suitable option.

Upvotes: 1

Views: 486

Answers (2)

davbryn
davbryn

Reputation: 7176

You need to store a reference to the child you want to remove (i.e a pointer to it in your @interface).

I'm fairly sure you can assign a CCNode a tag too, so it might be worth looking into that. You can then remove the node based on the tag you assign to it. Both are accessed with removeChild from the parent node.

Upvotes: 1

Coder404
Coder404

Reputation: 742

If you want to remove a subclass of the CCSprite (in this case CCMenuItemImage) then you use the removeChild method from the CCLayer class. In this case:

-(void)onRetry{

[self removeChild:lvl1];//This would remove the CCMenuItemImage object called lvl1.


    [m_sGo runAction:[CCSequence actions:actionMove, [CCCallFunc actionWithTarget:self selector:@selector(GoMoveSecond)], nil]];
}

Upvotes: 1

Related Questions