cocos2d-x版本,2.2
cocostudio版本:1.0.2.0
使用cocos2d-x 2.1.3没有成功,cocos2d-x 2.2版本内嵌cocostdio了,所以用2.2
使用cocostudio新建了一个项目,名字“Ma”,里面有两个控件,一个Button,名字“Button”。一个TextView,名字“text”
cpp
原文链接: http://www.cnblogs.com/android-qian/p/3425906.html
cocostudio版本:1.0.2.0
使用cocos2d-x 2.1.3没有成功,cocos2d-x 2.2版本内嵌cocostdio了,所以用2.2
使用cocostudio新建了一个项目,名字“Ma”,里面有两个控件,一个Button,名字“Button”。一个TextView,名字“text”
然后把导出的cocostudio项目添加到vc项目的resource中,然后是代码.h
#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__
cpp
#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;
}
};
原文链接: http://www.cnblogs.com/android-qian/p/3425906.html