Objective c 用于iphone游戏的cocos2d自定义本地化系统
我在游戏中使用a;在那个教程中,他在自定义方法中添加了标签,但我的文本标签是在init中添加的 本教程的示例:Objective c 用于iphone游戏的cocos2d自定义本地化系统,objective-c,localization,cocos2d-iphone,localizable.strings,Objective C,Localization,Cocos2d Iphone,Localizable.strings,我在游戏中使用a;在那个教程中,他在自定义方法中添加了标签,但我的文本标签是在init中添加的 本教程的示例: - (void) setHelloWorldLabel { // create and initialize a Label CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32];
- (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];
}
如何处理多个文本标签
一些代码示例可以帮助我:)一个解决方案是给每个标签一个不同的
标记,创建一个字典,使用标记作为键,字符串作为值。然后,遍历字典中的每个键(标记),并使用它检索每个CCLabel
(通过getChildByTag:
)。最后,在每个CCLabel
上调用setString:
,以更新新本地化的字符串。您可以添加另一个方法,该方法可以在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
}
你的意思是问当有多个标签带有标签50
时该怎么办?@jonsibley:不,我的意思是当我有多个文本标签时;不同的手帕!但是对于我的图层,我在其中的一些图层上还放置了一些CCMenuItem
s和CCMenuItemsAndSprite
(标签和背景图像)。它得到了它的子项,但我收到:'nsinternalinconsistenceexception',原因:'objectAtIndex(14)中的索引超出范围,索引15'
请发布您的代码图片,可能您需要递归之类的通用解决方案。您还可以尝试通过标记属性手动处理它们,或者将所有localazable对象放入可变字典中。