cocos2d-x 创建文字按钮的实现是怎样做到的,空口白话也没人看。我们直接看,通过下面的一段程序就可以实现cocos2d-x 创建文字按钮了。
  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3. using namespace cocos2d;
  4. using namespace CocosDenshion;
  5. CCScene* HelloWorld::scene()
  6. {
  7. // 'scene' is an autorelease object
  8. CCScene *scene = CCScene::create();
  9. // 'layer' is an autorelease object
  10. HelloWorld *layer = HelloWorld::create();
  11. // add layer as a child to scene
  12. scene->addChild(layer);
  13. // return the scene
  14. return scene;
  15. }
  16. // on "init" you need to initialize your instance
  17. bool HelloWorld::init()
  18. {
  19. //////////////////////////////
  20. // 1. super init first
  21. if ( !CCLayer::init() )
  22. {
  23. return false;
  24. }
  25. /////////////////////////////
  26. // 2. add a menu item with "X" image, which is clicked to quit the program
  27. //    you may modify it.
  28. // add a "close" icon to exit the progress. it's an autorelease object
  29. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  30. "CloseNormal.png",
  31. "CloseSelected.png",
  32. this,
  33. menu_selector(HelloWorld::menuCloseCallback) );
  34. pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
  35. // create menu, it's an autorelease object
  36. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  37. pMenu->setPosition( CCPointZero );
  38. this->addChild(pMenu, 1);
  39. /////////////////////////////
  40. // 3. add your codes below...
  41. // add a label shows "Hello World"
  42. // create and initialize a label
  43. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);
  44. // ask director the window size
  45. CCSize size = CCDirector::sharedDirector()->getWinSize();
  46. // position the label on the center of the screen
  47. pLabel->setPosition( ccp(size.width / 2, size.height - 20) );
  48. // add the label as a child to this layer
  49. this->addChild(pLabel, 1);
  50. // add "HelloWorld" splash screen"
  51. CCSprite* pSprite = CCSprite::create("HelloWorld.png");
  52. // position the sprite on the center of the screen
  53. pSprite->setPosition( ccp(size.width/2, size.height/2) );
  54. // add the sprite as a child to this layer
  55. this->addChild(pSprite, 0);
  56. CCMenuItemFont::setFontSize(32);    //设置字号
  57. CCMenuItemFont::setFontName("Marker Felt"); //设置字体
  58. CCMenuItemFont *font1=CCMenuItemFont::create("hah", this, menu_selector(HelloWorld::menuCloseCallback1));//添加文字按钮,点击实现缩放
  59. font1->setColor(ccc3(255, 0, 0));  //设置颜色
  60. CCMenuItemToggle * font2=CCMenuItemToggle::createWithTarget(this,menu_selector(HelloWorld::menuCloseCallback2),CCMenuItemFont::create("on"),CCMenuItemFont::create("off"),NULL);   //添加选择按钮,点击实现on与off之间切换
  61. font2->setColor(ccc3(0, 255, 0));
  62. font2->setSelectedIndex(1);    //设置默认显示,从零算起(此为off)
  63. CCMenu *newmenu=CCMenu::create(font1,font2,NULL);  // 此步很关键,否则只是菜单项,但不能点击
  64. newmenu->setPosition(ccp(size.width/2, size.height/2));
  65. newmenu->alignItemsVertically();     //设置为竖排排列
  66. this->addChild(newmenu,2);
  67. num=1;
  68. return true;
  69. }
  70. void HelloWorld::menuCloseCallback1(CCObject *pSender)
  71. {
  72. CCMenuItemFont *font=(CCMenuItemFont*)pSender;
  73. num++;    //hello类的私有成员
  74. num%=2;
  75. if(num==1)
  76. {
  77. font->setScale(2);   //设置缩放
  78. }
  79. else
  80. {
  81. //font->setFontSize(18);
  82. font->setScale(0.5);
  83. }
  84. }
  85. void HelloWorld::menuCloseCallback2(CCObject *pSender)
  86. {
  87. }
  88. void HelloWorld::menuCloseCallback(CCObject* pSender)
  89. {
  90. CCDirector::sharedDirector()->end();
  91. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  92. exit(0);
  93. #endif
  94. }

运行效果:

cocos2d-x 创建文字按钮