【问题标题】:cocos2d custom localization system for iphone gamesiphone游戏的cocos2d自定义本地化系统
【发布时间】:2012-04-05 09:41:07
【问题描述】:

我正在为我的游戏使用custom localization system;在那个教程中,他在自定义方法中添加了标签,但我的文本标签是在 init 中添加的

教程示例:

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

如何处理多个文本标签?

一些代码示例会帮助我:)

【问题讨论】:

  • 你的意思是问当有带有标签50的multipla标签时该怎么办?
  • @jonsibley:不,我的意思是当我有多个文本标签时;不同的

标签: objective-c localization cocos2d-iphone localizable.strings


【解决方案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
}

【讨论】:

  • 谢谢!但是对于其中一些图层,我还放置了一些CCMenuItems 和CCMenuItemsAndSprite(标签和背景图像)。它得到了它的孩子,但我收到了:'NSInternalInconsistencyException', reason: 'index out of range in objectAtIndex(14), index 15'
  • 请发布您的代码,可能您需要诸如递归之类的通用解决方案。您也可以尝试通过标签属性手动处理它们或将所有可本地化的对象放入可变字典中。
【解决方案2】:

一种解决方案是给每个标签一个不同的tag,创建一个字典,使用标签作为键,字符串作为值。然后,遍历字典中的每个键(标签)并使用它来检索每个CCLabel(通过getChildByTag:)。最后,在每个 CCLabel 上调用 setString: 以更新新本地化的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多