0

我爲我的遊戲使用了custom localization system;該教程中,他增加了標籤,自定義方法,但我的文字標籤在初始化cocos2d iphone遊戲定製本地化系統

教程的例子說:

- (void) setHelloWorldLabel 
{ 
    // create and initialize a Label 
    CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32]; 

    // ask director the the window size 
    CGSize size = [[CCDirector sharedDirector] winSize]; 

    // position the label on the center of the screen 
    label.position = ccp(size.width /2 , size.height/2); 

    //Check if it's already been added to the layer. 
    if ([self getChildByTag:50]) 
     [self removeChildByTag:50 cleanup:YES]; 

    // add the label as a child to this Layer 
    [self addChild:label z:0 tag:50]; 
} 

設置語言

-(void) menuCallbackEN: (id) sender 
{ 
    LocalizationSetLanguage(@"English"); 
    [self setHelloWorldLabel]; 
} 

如何處理多個文本標籤?

一些代碼示例將幫助我:)

+0

您的意思是當標籤爲'50'的多標籤標籤出現問題時該怎麼辦? – jonsibley 2012-04-05 12:41:56

+0

@jonsibley:不,我的意思是當我有多個文本標籤時;不同的人 – 2012-04-05 12:46:42

回答

1

您可以添加可以在init和語言更改事件上調用的另一種方法。 這種方法應該看起來像這樣:

- (void)initLocalizableLables 
{ 
    // Remove old labels 
    for (NSInteger i=[children_ count]-1; i>=0; i--) 
    { 
     CCNode *c = [children_ objectAtIndex:i]; 

     if ([c isKindOfClass:[CCLabel class]]) 
     { 
      [c removeFromParentAndCleanup:YES]; 
     } 
    } 

    // Add labels with localization  
    CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32]; 
    ... 
    [self addChild:label z:0 tag:50]; 
} 

- (void)init 
{ 
    ... 
    [self initLocalizableLables]; // add localized labels 
    ... 
} 

- (void)languageDidChange 
{ 
    [self initLocalizableLables]; // remove old localized labels and add new 
} 
+0

謝謝!但對於我的圖層,我也放置了一些'CCMenuItem'和'CCMenuItemsAndSprite'(標籤和背景圖片)。它得到它的孩子,但我收到:''NSInternalInconsistencyException',原因:'indexA超出範圍objectAtIndex(14),索引15'' – 2012-04-10 09:34:53

+0

請張貼你的代碼片,可能你需要類似遞歸的通用解決方案。您也可以嘗試通過標記屬性手動處理它們,或將所有可定位對象放入可變字典中。 – 2012-04-27 19:05:11

0

一種解決方案是給每個標籤不同tag,創建使用標籤,因爲他們鍵和字符串值的字典。然後,遍歷字典中的每個鍵(標籤),並使用它來檢索每個CCLabel(通過getChildByTag:)。最後,在每個CCLabel上調用setString:更新新本地化的字符串。

+0

謝謝,你能給我一個代碼示例的例子嗎? – 2012-04-09 14:28:13