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

Cocos2d-x 在屏幕上渲染文字

2013年12月08日 ⁄ 综合 ⁄ 共 3563字 ⁄ 字号 评论关闭

转自:http://blog.csdn.net/zhy_cheng/article/details/8268814

 

原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/8268814

这篇博文主要展示在屏幕上渲染文字,先来看看最后要实现的界面:

这个界面就是在屏幕的四个角落有不同字体不同颜色的Welcome来渲染,在屏幕的中间是汉字,颜色也是随机的。

下面来看看代码是怎么样实现的。

在main.cpp中将屏幕的大小设置为

  1. eglView->setFrameSize(800, 480);//设置界面大小
  1. eglView->setFrameSize(800, 480);//设置界面大小  


在AppDelegate.cpp的applicationDidFinishLaunching函数中

  1. pDirector->setDisplayStats(0);
  1. pDirector->setDisplayStats(0);  

不显示FPS

在HelloWorldSecne.cpp中,将init方法中的代码注释一部分,如下:

  1. bool HelloWorld::init()
  2. {
  3. bool bRet
    false;
  4. do
  5. {
  6. //////////////////////////////////////////////////////////////////////////
  7. // super init first
  8. //////////////////////////////////////////////////////////////////////////
  9. CC_BREAK_IF(! CCLayer::init());
  10. //////////////////////////////////////////////////////////////////////////
  11. // add your codes below...
  12. //////////////////////////////////////////////////////////////////////////
  13. // 1. Add a menu item with "X" image, which is
    clicked to quit the program.
  14. // Create a "close" menu item with close icon,
    it's an auto release object.
  15. /*CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  16. "CloseNormal.png",
  17. "CloseSelected.png",
  18. this,
  19. menu_selector(HelloWorld::menuCloseCallback));
  20. CC_BREAK_IF(! pCloseItem);
  21. // Place the menu item bottom-right conner.
  22. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width
    - 20, 20));
  23. // Create a menu with the "close" menu item, it's an auto
    release object.
  24. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  25. pMenu->setPosition(CCPointZero);
  26. CC_BREAK_IF(! pMenu);
  27. // Add the menu to HelloWorld layer as a child layer.
  28. this->addChild(pMenu, 1);
  29. // 2. Add a label shows "Hello World".
  30. // Create a label and initialize with string "Hello World".
  31. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World",
    "Arial", 24);
  32. CC_BREAK_IF(! pLabel);
  33. // Get window size and place the label upper.
  34. CCSize size = CCDirector::sharedDirector()->getWinSize();
  35. pLabel->setPosition(ccp(size.width / 2, size.height -
    50));
  36. // Add the label to HelloWorld layer as a child layer.
  37. this->addChild(pLabel, 1);
  38. // 3. Add add a splash screen, show the cocos2d splash
    image.
  39. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  40. CC_BREAK_IF(! pSprite);
  41. // Place the sprite on the center of the screen
  42. pSprite->setPosition(ccp(size.width/2, size.height/2));
  43. // Add the sprite to HelloWorld layer as a child layer.
  44. this->addChild(pSprite, 0);
  45. */
  46. bRet = true;
  47. while (0);
  48. return bRet;
  49. }
  1. bool HelloWorld::init()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6.         //////////////////////////////////////////////////////////////////////////  
  7.         // super init first  
  8.         //////////////////////////////////////////////////////////////////////////  
  9.   
  10.         CC_BREAK_IF(! CCLayer::init());  
  11.   
  12.         //////////////////////////////////////////////////////////////////////////  
  13.         // add your codes below...  
  14.         //////////////////////////////////////////////////////////////////////////  
  15.   
  16.         // 1. Add a menu item with "X" image, which is clicked to quit the program.  
  17.   
  18.         // Create a "close" menu item with close icon, it's an auto release object.  
  19.         /*CCMenuItemImage *pCloseItem = CCMenuItemImage::create( 
  20.             "CloseNormal.png", 
  21.             "CloseSelected.png", 
  22.             this, 
  23.             menu_selector(HelloWorld::menuCloseCallback)); 
  24.         CC_BREAK_IF(! pCloseItem); 
  25.  
  26.         // Place the menu item bottom-right conner. 
  27.         pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)); 
  28.  
  29.         // Create a menu with the "close" menu item, it's an auto release object. 
  30.         CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); 

抱歉!评论已关闭.