在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。

开发人员:Jason's.Alex QQ:531401335

csdn博客:http://blog.csdn.net/rushrooM


[cpp] view plain copy
  1. #include"cocos2d.h"
  2. usingnamespacecocos2d;
  3. classstrokeLabelTTF:publiccocos2d::CCNode
  4. {
  5. public:
  6. strokeLabelTTF();
  7. ~strokeLabelTTF();
  8. public:
  9. staticstrokeLabelTTF*create(constchar*string,constchar*fontName,floatfontSize,floatstrokeSize,constcocos2d::ccColor3B&colstroke=cocos2d::ccc3(0,0),cocos2d::CCTextAlignmenthAlignment=cocos2d::kCCTextAlignmentCenter,cocos2d::CCVerticalTextAlignmentvAlignment=cocos2d::kCCVerticalTextAlignmentTop);
  10. boolinitWithString(constchar*label,floatfstrokeSize,constcocos2d::ccColor3B&colstroke,cocos2d::CCTextAlignmenthAlignment,cocos2d::CCVerticalTextAlignmentvAlignment);
  11. staticstrokeLabelTTF*create(constchar*string,floatfontSize);
  12. public:
  13. voidsetColor(constcocos2d::ccColor3B&color3);
  14. voidsetString(constchar*label);
  15. voidsetstrokeColor(cocos2d::ccColor3Bcol){m_colstroke=col;updatestroke();}
  16. voidsetstrokeSize(floatstrokeSize){m_fstrokeSize=strokeSize;updatestroke();}
  17. voidsetAnchorPoint(constcocos2d::CCPoint&anchorPoint);
  18. protected:
  19. voidupdatestroke();
  20. private:
  21. floatm_fstrokeSize;
  22. cocos2d::ccColor3Bm_colstroke;
  23. cocos2d::CCSprite*m_sprite;
  24. cocos2d::cclabelTTF*m_label;
  25. CCPointanchorPoint;
  26. };

[cpp] view plain copy
  1. #include"strokeLabelTTF.h"
  2. USING_NS_CC;
  3. strokeLabelTTF::strokeLabelTTF()
  4. :m_colstroke(ccc3(0,0))
  5. ,m_fstrokeSize(1.0f)
  6. ,m_sprite(NULL)
  7. ,m_label(NULL)
  8. ,anchorPoint(0.5,0.5)
  9. {}
  10. strokeLabelTTF::~strokeLabelTTF()
  11. {
  12. CC_SAFE_RELEASE_NULL(m_label);
  13. }
  14. boolstrokeLabelTTF::initWithString(constchar*string,CCTextAlignmenthAlignment,CCVerticalTextAlignmentvAlignment)
  15. {
  16. m_fstrokeSize=strokeSize;
  17. m_colstroke=colstroke;
  18. m_label=cclabelTTF::create(string,fontName,fontSize,CCSizeZero,hAlignment,vAlignment);
  19. m_label->retain();
  20. updatestroke();
  21. returntrue;
  22. }
  23. strokeLabelTTF*strokeLabelTTF::create(constchar*string,CCVerticalTextAlignmentvAlignment)
  24. {
  25. strokeLabelTTF*pRet=newstrokeLabelTTF();
  26. if(pRet&&pRet->initWithString(string,fstrokeSize,colstroke,vAlignment))
  27. {
  28. pRet->autorelease();
  29. returnpRet;
  30. }
  31. CC_SAFE_DELETE(pRet);
  32. returnNULL;
  33. }
  34. strokeLabelTTF*strokeLabelTTF::create(constchar*string,floatfontSize)
  35. {
  36. strokeLabelTTF*pRet=newstrokeLabelTTF();
  37. if(pRet&&pRet->initWithString(string,1.5,ccc3(0,kCCTextAlignmentCenter,kCCVerticalTextAlignmentTop))
  38. {
  39. pRet->autorelease();
  40. returnpRet;
  41. }
  42. CC_SAFE_DELETE(pRet);
  43. returnNULL;
  44. }
  45. voidstrokeLabelTTF::updatestroke()
  46. {
  47. if(m_sprite)
  48. {
  49. removeChild(m_sprite,true);
  50. }
  51. CCSizetextureSize=m_label->getContentSize();
  52. textureSize.width+=2*m_fstrokeSize;
  53. textureSize.height+=2*m_fstrokeSize;
  54. //calltoclearerror
  55. glGetError();
  56. CCRenderTexture*rt=CCRenderTexture::create(textureSize.width,textureSize.height);
  57. if(!rt)
  58. {
  59. cclOG("createrendertextureFailed!!!!");
  60. addChild(m_label);
  61. return;
  62. }
  63. ccColor3Bcol=m_label->getColor();
  64. m_label->setColor(m_colstroke);
  65. ccBlendFuncoriginalBlend=m_label->getBlendFunc();
  66. ccBlendFuncfunc={GL_SRC_ALPHA,GL_ONE};
  67. m_label->setBlendFunc(func);
  68. m_label->setAnchorPoint(ccp(0.5f,0.5f));
  69. rt->begin();
  70. for(inti=0;i<360;i+=15)
  71. {
  72. floatr=CC_degrees_TO_radians(i);
  73. m_label->setPosition(ccp(
  74. textureSize.width*0.5f+sin(r)*m_fstrokeSize,
  75. textureSize.height*0.5f+cos(r)*m_fstrokeSize));
  76. m_label->visit();
  77. }
  78. m_label->setColor(col);
  79. m_label->setBlendFunc(originalBlend);
  80. m_label->setPosition(ccp(textureSize.width*0.5f,textureSize.height*0.5f));
  81. m_label->visit();
  82. rt->end();
  83. CCTexture2D*texture=rt->getSprite()->getTexture();
  84. texture->setAliasTexParameters();
  85. m_sprite=CCSprite::createWithTexture(rt->getSprite()->getTexture());
  86. setContentSize(m_sprite->getContentSize());
  87. m_sprite->setAnchorPoint(this->anchorPoint);//------------
  88. m_sprite->setPosition(ccp(0,0));
  89. ((CCSprite*)m_sprite)->setFlipY(true);
  90. addChild(m_sprite);
  91. }
  92. voidstrokeLabelTTF::setString(constchar*label)
  93. {
  94. if(m_label)
  95. {
  96. if(0!=strcmp(label,m_label->getString()))
  97. {
  98. m_label->setString(label);
  99. updatestroke();
  100. }
  101. }
  102. else
  103. {
  104. cclOG("ERROR:cclabelTTFstroke::setStringm_label=NULL");
  105. }
  106. }
  107. voidstrokeLabelTTF::setColor(constccColor3B&color3)
  108. {
  109. if(m_label)
  110. {
  111. ccColor3Bcol=m_label->getColor();
  112. if(color3.r!=col.r||color3.g!=col.g||color3.b!=col.b)
  113. {
  114. m_label->setColor(color3);
  115. updatestroke();
  116. }
  117. }
  118. else
  119. {
  120. cclOG("ERROR:cclabelTTFstroke::setColorm_label=NULL");
  121. }
  122. }
  123. voidstrokeLabelTTF::setAnchorPoint(constcocos2d::CCPoint&anchorPoint1)
  124. {
  125. this->anchorPoint=anchorPoint1;
  126. updatestroke();
  127. }

cocos2d实现CCLabelTTF真正字体描边效果的更多相关文章

  1. 详解iOS游戏开发中Cocos2D的坐标位置关系

    这篇文章主要介绍了iOS游戏开发中Cocos2D的坐标位置关系,Cocos2D是专门用来开发iOS游戏的开源框架,文中示例代码采用Objective-C语言,需要的朋友可以参考下

  2. 剖析iOS开发中Cocos2d-x的内存管理相关操作

    这篇文章主要介绍了剖析iOS开发中Cocos2d-x的内存管理相关操作,Cocos2d-x是开发游戏的利器,需要的朋友可以参考下

  3. iOS开发中使用cocos2d添加触摸事件的方法

    这篇文章主要介绍了iOS开发中使用cocos2d添加触摸事件的方法,cocos2d是制作iOS游戏的利器,需要的朋友可以参考下

  4. ios – cocos2d 2.0-rc2:结束导演并重启

    我有一个使用UIKitmenues的cocos2d驱动游戏,所以我只使用一个viewcontroller的框架,这是游戏本身.此外,它只有一个场景.由于cocos2d2.0导演本身是一个UIViewController子类,所以我只需在用户点击一个开始按钮时将其推送到我的MenuViewController中:当用户启动第一个游戏时,第一次调用该方法时,这很好.当游戏结束时,我调用[[CCDire

  5. cocos2dx之飞机大战简单版1

    先说下版本vs2010+cocos2dx2.2本章主要是告诉大家如何实现创建背景、飞机、***精灵,并且然后他们动起来,然后做一个碰撞测试,当***和敌方飞机碰撞时就销毁精灵并且加一个爆炸的精灵。所以我们只需要在GameLogic中调用addMonster,在GameLogicaddBullet1中调用addBullet1就可以了注意一定要在参数中加上floatdt,它是表明多少秒调用一次这个函数。

  6. cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing file.

    程序运行的时候输出这条信息cocos2d:fullPathForFilename:Nofilefoundat/cc_2x2_white_image.Possiblemissingfile.并没有影响程序正常运行产生的原因可能是某个精灵未配置纹理导致的,不需要关心。参考cc_2x2_white_imageismissing,givingwarningatruntime:http://www.coco

  7. Cocos2dx学习笔记3---LittleRunner

    4.创建场景类5启动6.不要在写代码的时候,把所有的功能都往一个类里面写,多分开几个类来写,多写一些注释,以便后面要修改或者查阅看不懂。

  8. 寒風的Cocos2dx之旅之添加按钮

    要在Cocos2d中使用Button,首先引入头文件#include"ui/CocosGUI.h"usingnamespaceuil;引入完毕后,就创建button。功能要比MenuItem要强大的多。他可以使按钮放大等功能。

  9. cocos2d实现CCLabelTTF真正字体描边效果

    在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。

  10. cocos2d刚体会被穿透

    在调试Coco2d-javaEditor的BoxShape刚体碰撞时,发现有时候没有碰撞效果,直接穿透过去了。后来发现,刚体碰撞必须在没有外在干扰下才正常,而我每次碰撞的时候,都执行了Action动作,这个action动作会优先于碰撞,所以,会先执行action,这个action导致碰撞无效。为了防止穿透,最好不要用action。

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部