现在的位置: 首页 > 综合 > 正文

cocos2d-x 使用CCLabelAtlas自定义字体

2013年10月03日 ⁄ 综合 ⁄ 共 1804字 ⁄ 字号 评论关闭

原创文章,欢迎转载!转载请注明: 转载自Cocos2D开发网--Cocos2Dev.com

原文链接地址: cocos2d-x
使用CCLabelAtlas自定义字体

有时候游戏中要用到一些特殊的字体效果,特别是数字。

CCLabelAtlas就可以从png图中读取文字。

CCLabelAtlas* diceCount=CCLabelAtlas::labelWithString("1:", "nums_font.png", 14, 21, '0');

第一个参数:显示的内容:1x,你也许会奇怪为什么是1x,因为使用的png图必须是连续的,因为程序内部是议连续的scall码识别的。9的后一位的”:“,所以先实现x就得用”:“代替。

第二个参数:图片的名字

第三个参数:每一个数字的宽

第四个参数:每一个数字的高

每五个数字:开始字符


测试代码:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCLabelAtlas *label = CCLabelAtlas::create("0 1 2 3 4 5 6 7 8 9 x:", "set1_player_hud3.png", 14, 21, '0');
    label->setPosition(ccp(10, 200));
    label->setColor(ccRED);
    label->setOpacity(200);
    this->addChild(label);
    
    CCActionInterval *fade = CCFadeOut::create(1.0f);
    CCActionInterval *fade_in = fade->reverse();
    CCFiniteTimeAction *seq = CCSequence::create(fade,fade_in,NULL);
    CCAction *repeat = CCRepeatForever::create((CCActionInterval *)seq);
    label->runAction(repeat);
    return true;
}

原创文章,欢迎转载!转载请注明: 转载自Cocos2D开发网--Cocos2Dev.com

原文链接地址: cocos2d-x
使用CCLabelAtlas自定义字体

有时候游戏中要用到一些特殊的字体效果,特别是数字。

CCLabelAtlas就可以从png图中读取文字。

CCLabelAtlas* diceCount=CCLabelAtlas::labelWithString("1:", "nums_font.png", 14, 21, '0');

第一个参数:显示的内容:1x,你也许会奇怪为什么是1x,因为使用的png图必须是连续的,因为程序内部是议连续的scall码识别的。9的后一位的”:“,所以先实现x就得用”:“代替。

第二个参数:图片的名字

第三个参数:每一个数字的宽

第四个参数:每一个数字的高

每五个数字:开始字符


测试代码:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCLabelAtlas *label = CCLabelAtlas::create("0 1 2 3 4 5 6 7 8 9 x:", "set1_player_hud3.png", 14, 21, '0');
    label->setPosition(ccp(10, 200));
    label->setColor(ccRED);
    label->setOpacity(200);
    this->addChild(label);
    
    CCActionInterval *fade = CCFadeOut::create(1.0f);
    CCActionInterval *fade_in = fade->reverse();
    CCFiniteTimeAction *seq = CCSequence::create(fade,fade_in,NULL);
    CCAction *repeat = CCRepeatForever::create((CCActionInterval *)seq);
    label->runAction(repeat);
    return true;
}

抱歉!评论已关闭.