0
votes

I made an App using cocos2D that has a hierarchical structure.

And I used a UIButton on glView in a child layer.

When I get back to parent layer, the button still on it's position.

I want to terminate that.

How can I do this?

Here's the code.

MainHallLayer.m

- (id)init
{
    self = [super init];

    if (self != nil)
    {
        CCMenu *menuLists = [CCMenu menuWithItems: nil];
        menuLists.position = ccp(screenSize.width/2, screenSize.height/2);
        [self addChild:menuLists z:10];

        NSString *fontName = [NSString stringWithFormat:@"Chalkboard SE"];
        CGFloat fontSize = 28;

        {
            CCLabelTTF *label = [CCLabelTTF labelWithString:@"Touch Field : Getting 
                            touches coordinates" fontName:fontName fontSize:fontSize];
            [label setColor:ccc3(0.0, 0.0, 0.0)];
            label.anchorPoint = ccp(0, 0.5f);
            CCMenuItem *menuItem = [CCMenuItemLabel itemWithLabel:label block:^(id 
                                                                            sender)
            {
                CCScene *scene = [TouchField1Layer node];
                [ReturningNode returnToNodeWithParent:scene];
                [[CCDirector sharedDirector] replaceScene:scene];
            }];
            [menuLists addChild:menuItem];
        }
    }
    return self;
}

ReturnToNode.m - (id)initWithParentNode:(CCNode *)parentNode { self = [super init];

    if (self != nil) 
    {
        CGSize screenSize = [[CCDirector sharedDirector]winSize];

        CCLabelTTF *label = [CCLabelTTF labelWithString:@"  <= Return" 
                                               fontName:@"Gill Sans" 
                                               fontSize:30.0f];
        label.color = ccMAGENTA;

        id tint_1 = [CCTintTo actionWithDuration:0.333f red:1.0 green:.0f blue:.0f];
        id tint_2 = [CCTintTo actionWithDuration:0.333f red:.5f green:.5f blue:.0f];
        id tint_3 = [CCTintTo actionWithDuration:0.333f red:.0f green:1.0 blue:.0f];
        id tint_4 = [CCTintTo actionWithDuration:0.333f red:.0f green:.5f blue:.5f];
        id tint_5 = [CCTintTo actionWithDuration:0.333f red:.0f green:.0f blue:1.0];
        id tint_6 = [CCTintTo actionWithDuration:0.333f red:.5f green:.0f blue:.5f];

        id sequence = [CCSequence actions:tint_1,tint_2,tint_3,tint_4,tint_5,tint_6, nil];
        id repeatAction = [CCRepeatForever actionWithAction:sequence];
        [label runAction:repeatAction];

        CCLayerColor *labelBackground = [CCLayerColor layerWithColor:ccc4(0.0, 0.0, 70, 40) width:label.contentSize.width + 20 height:label.contentSize.height + 20];

        [label addChild:labelBackground z:-1];

        CCMenuItem *menuItem = [CCMenuItemLabel itemWithLabel:label block:^(id sender)
        {
            [self removeFromParentAndCleanup:YES];
            [[CCDirector sharedDirector] replaceScene:[TouchControllerLayer scene]];
        }];

        CCMenu *menu = [CCMenu menuWithItems:menuItem, nil];
        [menu alignItemsVertically];
        menu.position = CGPointMake(label.contentSize.width/2 , screenSize.height - 20);
        [self addChild:menu];

        [parentNode addChild:self z:1000];
    }
    return self;
}

+ (id)returnToNodeWithParent:(CCNode *)parentNode
{
    return [[[self alloc] initWithParentNode:parentNode] autorelease];
}

TouchField1Layer.m - (id)init { self = [super init];

    if (self != nil) 
    {
        BackgroundLayer *background = [BackgroundLayer node];
        TouchField3Layer *layer = [TouchField3Layer node];

        [self addChild:background z:3];
        [self addChild:layer z:2];

        [self preparingTools];
    }
    return self;
}

- (void)preparingTools
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(0, 0, 100, 40);

    UIView *glView = [CCDirector sharedDirector].openGLView;
    glView.tag = TAG_GLVIEW;
    [glView addSubview:button];
}

Any advices, helps are welcome. Always thanks. Bless You.

1

1 Answers

0
votes

If you add UIKit views you will have to remove them manually at the appropriate point in time. Cocos2D does not manage the lifetime of UIKit views, only classes derived from CCNode.

For example when changing scenes you will have to remove the UIButton from the glView either in -(void) dealloc or in -(void) cleanup.