cocos2d-x版本,2.2node
cocostudio版本:1.0.2.0json
使用cocos2d-x 2.1.3沒有成功,cocos2d-x 2.2版本內嵌cocostdio了,因此用2.2iphone
使用cocostudio新建了一個項目,名字「Ma」,裏面有兩個控件,一個Button,名字「Button」。一個TextView,名字「text」編輯器
而後把導出的cocostudio項目添加到vc項目的resource中,而後是代碼.hthis
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC_EXT; class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommand to return the exactly class pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); void touchEvent(CCObject *pSender, TouchEventType type); void countrytouch(CCObject*pSender,TouchEventType type); // implement the "static node()" method manually UILabel*mText; UILabel*mCountryLabel; CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
.ccpspa
#include "HelloWorldScene.h" using namespace cocos2d; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { scene = CCScene::create(); CC_BREAK_IF(! scene); HelloWorld *layer = HelloWorld::create(); CC_BREAK_IF(! layer); scene->addChild(layer); } while (0); return scene; } bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //建立一個畫布 UILayer* ul =UILayer::create(); //把畫布添加到場景中 this->addChild(ul); //建立一個文本框 mText=UILabel::create(); mText->setText("text"); mText->setFontName(""); mText->setFontSize(32); mText->setAnchorPoint(ccp(0.5f, -1)); mText->setPosition(ccp(100,100)); ul->addWidget(mText); //建立一個Button按鈕 UIButton*playBtn=UIButton::create(); playBtn->setTouchEnable(true); playBtn->setTag(1); playBtn->loadTextures("image/btn-play-normal.png","image/btn-play-selected.png",""); playBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchEvent)); playBtn->setPosition(ccp(50,50)); ul->addWidget(playBtn); //調用UI編輯器編輯的按鈕 //把UI編輯的地圖添加到畫布中 UIWidget*pUI=CCUIHELPER->createWidgetFromJsonFile("Ma.json"); ul->addWidget(pUI); //獲取UI 上Button的的控件 UIWidget* countryBtn = UIHelper::instance()->seekWidgetByName(pUI,"Button"); //dynamic_cast<UIWidget*>(pUI) countryBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchEvent)); //獲取UI上的Label控件 mCountryLabel=(UILabel*)(UIHelper::instance()->seekWidgetByName(pUI,"text")); bRet = true; } while (0); return bRet; } void HelloWorld::menuCloseCallback(CCObject* pSender) { CCDirector::sharedDirector()->end(); } void HelloWorld::touchEvent(CCObject *pSender, TouchEventType type){ int tag=((UIWidget*)pSender)->getTag(); switch(tag){ case 1: switch(type){ case TOUCH_EVENT_BEGAN: mText->setText("1"); break; case TOUCH_EVENT_MOVED: mText->setText("2"); break; case TOUCH_EVENT_ENDED: mText->setText("3"); break; } break; case 8: switch(type){ case TOUCH_EVENT_BEGAN: mCountryLabel->setText("1"); break; case TOUCH_EVENT_MOVED: mCountryLabel->setText("2"); break; case TOUCH_EVENT_ENDED: mCountryLabel->setText("3"); break; } break; } };