cocos2d custom localization system for iphone games
-
29-05-2021 - |
题
I'm using a custom localization system for my game; in that tutorial he adds the label in a custom method but my text labels are added in init
Tutorial's example:
- (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];
}
Setting a language
-(void) menuCallbackEN: (id) sender
{
LocalizationSetLanguage(@"English");
[self setHelloWorldLabel];
}
How to deal with multiple text labels?
Some code sample would help me :)
解决方案
You could add another method which could be called on init and on language change events. This method should look like this:
- (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
}
其他提示
One solution is to give each label a different tag
, create a dictionary using the tags as they key and the string as the value. Then, iterate through each key (tag) in the dictionary and use it to retrieve each CCLabel
(via getChildByTag:
). Lastly, call setString:
on each CCLabel
to update the newly localized string.
不隶属于 StackOverflow