如何调用cocostudio创建场景中的按钮控件

PS:不知道原作者是谁,我转载的那个链接,没有标明原文链接,所以下面我只能给出个转载的链接。

转载自:http://blog.csdn.net/zhanghefu/article/details/19767927



首先使用cocostudio创建两个场景,在其中一个场景中添加一个了使用cocostudio创建的ui按钮控件,下面实例代码就是教你如何使用第一个场景中按钮切换到另一个场景的主要代码:

[cpp] view plain copy print ?
  1. 如何调用cocostudio创建场景中的按钮控件
  2.    cocos2d::gui::TouchGroup* touchGroup = static_cast<cocos2d::gui::TouchGroup*>(render->getNode());  
  3.     UIWidget* widget = static_cast<UIWidget*>(touchGroup->getWidgetByName("Panel_20"));  
  4.     UIButton* button = static_cast<UIButton*>(widget->getChildByName("Button_24"));  
  5.         //为按钮添加触控事件  
  6.     button->addTouchEventListener(this, toucheventselector(MenuScene::touchEvent));  
  7.   
  8.     return pNode;  
  9. }  
[cpp] view plain copy print ?
  1. cocos2d::CCNode* MenuScene::createGameScene()  
  2. {  
  3.     CCNode *pNode = SceneReader::sharedSceneReader()->createNodeWithSceneFile("yourJson.json");//加载第一个场景  
  4.     if (pNode == NULL)  
  5.     {  
  6.         return NULL;  
  7.     }  
  8.     _node = pNode;  
  9.   
  10.         //从场景中获取ui按钮控件  
  11.     CCComRender *render = static_cast<CCComRender*>(_node->getChildByTag(10013)->getComponent("GUIComponent"));  
  12.     cocos2d::gui::TouchGroup* touchGroup = static_cast<cocos2d::gui::TouchGroup*>(render->getNode());  
  13.     UIWidget* widget = static_cast<UIWidget*>(touchGroup->getWidgetByName("Panel_20"));  
  14.     UIButton* button = static_cast<UIButton*>(widget->getChildByName("Button_24"));  
  15.         //为按钮添加触控事件  
  16.     button->addTouchEventListener(this, toucheventselector(MenuScene::touchEvent));  
  17.   
  18.     return pNode;  
  19. }  


按钮单击相应函数如下:

[cpp] view plain copy print ?
  1. void MenuScene::touchEvent(CCObject *pSender, TouchEventType type)  
  2. {  
  3.     CCScene *pScene = GetReadyScene::scene();//创建你需要切换的场景对象  
  4.     CCDirector::sharedDirector()->replaceScene(pScene);  
  5. }  
[cpp] view plain copy print ?
  1. void MenuScene::touchEvent(CCObject *pSender, TouchEventType type)  
  2. {  
  3.     CCScene *pScene = GetReadyScene::scene();//创建你需要切换的场景对象  
  4.     CCDirector::sharedDirector()->replaceScene(pScene);  
  5. }  

对的,就是so easy,这也是我从TestCpp里面参考直接拿来使用的,现在网上这方面的资料还是相对较少些,所以当你不知道的时候多去参考里面的实例。以上代码的使用前提是你要对cocostudio的使用有一定熟悉,一些加载的头文件也得注意还要就是using namespace XXX的添加。

你可能感兴趣的:(cocos2d,cocostudio)