Cocos2d游戏开发5-Text, Fonts, and the Written Word

本文介绍Cocos2d-x中使用CCLabelTTF和CCLabelBMFont进行文本显示的方法,包括创建文本标签、设置位置与动作、调整锚点及对齐方式等,并提供了一种列出iOS可用字体名称的方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CCLabelTTF   


CCLabelTTF  *label = [CCLabelTTF labelWithString:@"Hello World"
fontName:@"Marker Felt" fontSize:64];

1. The CCLabelTTF object will use the CCTexture2D class to create an image texture from your text and display that texture onscreen.

2. After the initialization code above, you can use the CCLabelTTF like any other CCNode, add it to your layer, position it, and so forth. 

3. Each time you call the setText method, a new texture is created.


CCLabelTTF *gameBeginLabel =
         [CCLabelTTF labelWithString:@"Game Start" fontName:@"Helvetica" fontSize:64];           
[gameBeginLabel setPosition:ccp(screenSize.width/2,screenSize.height/2)];
[self addChild:gameBeginLabel];
id labelAction = [CCSpawn actions:
                   [CCScaleBy actionWithDuration:2.0f scale:4],
                    [CCFadeOut actionWithDuration:2.0f],
                    nil];
[gameBeginLabel runAction:labelAction];


Anchor Points and Alignment

1. The anchor point is what the texture to use to place it onscreen. Cocos2D defaults to an anchor point in the middle of your texture, so by default your objects are always placed according to their center points. 



2. Default anchor point at the middle of the texture would have a value of (0.5,0.5). Remember, anchor points range between zero and one, with (0,0) on the bottom left and (1,1) on the top right.

[gameBeginLabel setAnchorPoint: ccp(0, 0.5f)];

Listing the Fonts Available in iOS

NSMutableArray *fontNames = [[NSMutableArray alloc] init];
       NSArray *fontFamilyNames = [UIFont familyNames];
       for (NSString *familyName in fontFamilyNames) {
            NSLog(@"Font Family Name = %@", familyName);
            NSArray *names = [UIFont fontNamesForFamilyName:familyName];
            NSLog(@"Font Names = %@", fontNames);
            [fontNames addObjectsFromArray:names];
        }
   [fontNames release];


CCLabelBMFont

1. CCLabelTTFs are great for when you just need the standard iOS fonts and do not have to change the text of the labels very often. 

2. A bitmapped font atlas is an image containing all of the characters you want to dis- play along with coordinates data that allows the characters to be cut out of the master image. 

3. Since the characters are already stored as images, you can- not alter their size without using a different font atlas. 


 CCLabelBMFont *gameBeginLabel =
        [CCLabelBMFont labelWithString:@"Game Start"
         fntFile:@"SpaceVikingFont.fnt"];

Live Debugging

1. 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值