CCControlButton按钮也是cocos2d-x扩展的一个组件,还是应用程序中最常见、使用效率最高的组件
HelloWorld.h文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d::extension;
class HelloWorld : public cocos2d::CCLayer,public cocos2d::extension::CCEditBoxDelegate
{
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();
CREATE_FUNC(HelloWorld);
//按钮按下回调函数
void touchDownAction(CCObject* pSender,CCControlEvent controlEvent);
//按钮在内部抬起回调函数
void touchUpInsedeAction(CCObject* pSender,CCControlEvent controlEvent);
//按钮在外部抬起回调函数
void touchUpOutsideAction(CCObject* pSender,CCControlEvent controlEvent);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp文件
#include "HelloWorldScene.h"
#include "cocos-ext.h"
#include "cocos2d.h"
using namespace cocos2d;
using namespace cocos2d::extension;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
//
// super init first
//
CC_BREAK_IF(! CCLayer::init());
CCSize size = CCDirector::sharedDirector()->getWinSize();
//按钮
CCLabelTTF* pBtnLabel = CCLabelTTF::create("no","MarkerFelt",25);
CCControlButton* pBtn = CCControlButton::create(
pBtnLabel,
CCScale9Sprite::create("button.png")
);
CCControlButton* pBtn = CCControlButton::create(
CCScale9Sprite::create("button.png")
);
pBtn->setPosition(ccp(50,50));
//按钮被选中后背景图响应的状态
pBtn->setBackgroundSprite(CCScale9Sprite::create("buttonHighlighted.png"));
//按钮被选中后文字颜色响应的状态
pBtn->setTitleColorForState(ccc3(255,0,0),CCControlStateHighlighted);
//按钮被选中后文字状态响应的状态
pBtn->setTitleForState(
CCString::create("yes")
,CCControlStateHighlighted
);
//按钮按下事件回调
pBtn->addTargetWithActionForControlEvents(
this,
cccontrol_selector(HelloWorld::touchDownAction),
CCControlEventTouchDown
);
//按钮再其内部抬起事件回调
pBtn->addTargetWithActionForControlEvents(
this,
cccontrol_selector(HelloWorld::touchUpInsedeAction),
CCControlEventTouchUpInside
);
//按钮再其外部抬起事件回调
pBtn->addTargetWithActionForControlEvents(
this,
cccontrol_selector(HelloWorld::touchUpOutsideAction),
CCControlEventTouchUpOutside
);
setPreferredSize()//设置为图片大小
this->addChild(pBtn);
bRet = true;
} while (0);
return bRet;
}
//按钮按下回调函数
void HelloWorld::touchDownAction(CCObject* pSender,CCControlEvent controlEvent)
{
}
//按钮在内部抬起回调函数
void HelloWorld::touchUpInsedeAction(CCObject* pSender,CCControlEvent controlEvent)
{
}
//按钮在外部抬起回调函数
void HelloWorld::touchUpOutsideAction(CCObject* pSender,CCControlEvent controlEvent)
{
}