cocosStudio使用

前端之家收集整理的这篇文章主要介绍了cocosStudio使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用cocostudio新建了一个项目,名字“Ma”,里面有两个控件,一个Button,名字“Button”。一个TextView,名字“text”


然后把导出的cocostudio项目添加到vc项目的resource中,然后是代码.h




  1. #ifndef__HELLOWORLD_SCENE_H__
  2. #define__HELLOWORLD_SCENE_H__
  3. #include"cocos2d.h"
  4. #include"cocos-ext.h"
  5. USING_NS_CC_EXT;
  6. classHelloWorld:publiccocos2d::CCLayer
  7. {
  8. public:
  9. //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
  10. virtualboolinit();
  11. //there'sno'id'incpp,sowerecommandtoreturntheexactlyclasspointer
  12. staticcocos2d::CCScene*scene();
  13. //aselectorcallback
  14. voidmenuCloseCallback(CCObject*pSender);
  15. voidtouchEvent(CCObject*pSender,TouchEventTypetype);
  16. voidcountrytouch(CCObject*pSender,TouchEventTypetype);
  17. //implementthe"staticnode()"methodmanually
  18. UILabel*mText;
  19. UILabel*mCountryLabel;
  20. CREATE_FUNC(HelloWorld);
  21. };
  22. #endif//__HELLOWORLD_SCENE_H__

cpp

    #include"HelloWorldScene.h"
  1. usingnamespacecocos2d;
  2. CCScene*HelloWorld::scene()
  3. CCScene*scene=NULL;
  4. do
  5. {
  6. scene=CCScene::create();
  7. CC_BREAK_IF(!scene);
  8. HelloWorld*layer=HelloWorld::create();
  9. CC_BREAK_IF(!layer);
  10. scene->addChild(layer);
  11. }while(0);
  12. returnscene;
  13. }
  14. boolHelloWorld::init()
  15. boolbRet=false;
  16. do
  17. CC_BREAK_IF(!CCLayer::init());
  18. //创建一个画布
  19. UILayer*ul=UILayer::create();
  20. //把画布添加到场景中
  21. this->addChild(ul);
  22. //创建一个文本框
  23. mText=UILabel::create();
  24. mText->setText("text");
  25. mText->setFontName("");
  26. mText->setFontSize(32);
  27. mText->setAnchorPoint(ccp(0.5f,-1));
  28. mText->setPosition(ccp(100,100));
  29. ul->addWidget(mText);
  30. //创建一个Button按钮
  31. UIButton*playBtn=UIButton::create();
  32. playBtn->setTouchEnable(true);
  33. playBtn->setTag(1);
  34. playBtn->loadTextures("image/btn-play-normal.png","image/btn-play-selected.png","");
  35. playBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchEvent));
  36. playBtn->setPosition(ccp(50,50));
  37. ul->addWidget(playBtn);
  38. //调用UI编辑器编辑的按钮
  39. //把UI编辑的地图添加到画布中
  40. UIWidget*pUI=CCUIHELPER->createWidgetFromJsonFile("Ma.json");
  41. ul->addWidget(pUI);
  42. //获取UI上Button的的控件
  43. UIWidget*countryBtn=UIHelper::instance()->seekWidgetByName(pUI,"Button");
  44. //dynamic_cast<UIWidget*>(pUI)
  45. countryBtn->addTouchEventListener(//获取UI上的Label控件
  46. mCountryLabel=(UILabel*)(UIHelper::instance()->seekWidgetByName(pUI,"text"));
  47. bRet=true;
  48. }while(0);
  49. returnbRet;
  50. voidHelloWorld::menuCloseCallback(CCObject*pSender)
  51. CCDirector::sharedDirector()->end();
  52. }
  53. voidHelloWorld::touchEvent(CCObject*pSender,TouchEventTypetype){
  54. inttag=((UIWidget*)pSender)->getTag();
  55. switch(tag){
  56. case1:
  57. switch(type){
  58. caseTOUCH_EVENT_BEGAN:
  59. mText->setText("1");
  60. break;
  61. caseTOUCH_EVENT_MOVED:
  62. mText->setText("2");
  63. break;
  64. caseTOUCH_EVENT_ENDED:
  65. mText->setText("3");
  66. case8:
  67. switch(type){
  68. caseTOUCH_EVENT_BEGAN:
  69. mCountryLabel->setText("1");
  70. caseTOUCH_EVENT_MOVED:
  71. mCountryLabel->setText("2");
  72. caseTOUCH_EVENT_ENDED:
  73. mCountryLabel->setText("3");
  74. };
原文链接:https://www.f2er.com/cocos2dx/347017.html

猜你在找的Cocos2d-x相关文章