2011-12-28 16 views
0

我使用具有分層結構的cocos2D創建了一個App。在cocos2d中終止glView

而且我在一個子圖層的glView上使用了一個UIButton。

當我回到父層時,按鈕仍然位於它的位置。

我想終止那個。

我該怎麼做?

這是代碼。

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 { 自我= [超級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];

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]; 
} 

任何建議,幫助,歡迎。總是謝謝。祝福你。

回答

0

如果您添加UIKit視圖,您將不得不在適當的時間點手動刪除它們。 Cocos2D不管理UIKit視圖的生命週期,只管理從CCNode派生的類。

例如,在更改場景時,您必須從glView中刪除UIButton,其格式爲-(void) dealloc-(void) cleanup

+0

總是謝謝'LearnCocos2D':) – 2011-12-30 02:06:55