=================================.cpp文件
<pre name="code" class="cpp">bool HelloWorld::init() {
if ( !CCLayer::init() ) {
return false;
}
CCLabelTTF * label = CCLabelTTF::create("为选中文字", "MarkerFelt",25);
CCControlButton * button = CCControlButton ::create(label,CCScale9Sprite::create("button.png"));
button->setPosition(ccp(240, 170));
// 按钮选中后背景图响应的状态
button->setTitleColorForState(ccc3(255, 0, 0), CCControlStateHighlighted);
// 按钮选中后文字响应的状态
button->setTitleForState(CCString::create("选中文字"), CCControlStateHighlighted); addChild(button);
// 按下按钮事件回调
button->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);
// 按钮在其内部拾起事件回调
button->addTargetWithActionForControlEvents(this , cccontrol_selector( HelloWorld::touchUpInsideAction), CCControlEventTouchDragEnter);
// 按钮在其外部拾起事件回调 button->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchUpOutsideAction), CCControlEventTouchDragOutside);
// 用于显示按钮的状态
CCLabelTTF * la = CCLabelTTF ::create(" ", "MarkerFelt",20);
la->setColor(ccc3(255, 0, 0));
la->setPosition(ccp(240, 220));
addChild(la,0,923); return true;
}
// 按下按钮事件回调
void HelloWorld:: touchDownAction(CCObject * sender , CCControlEvent * controlEvent)
{
CCLabelTTF * label = (CCLabelTTF*) this ->getChildByTag(923);
label->setString(CCString::createWithFormat("按下")->getCString());
}
// 按钮在其内部抬起事件回调
void HelloWorld::touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent) {
CCLabelTTF * label = (CCLabelTTF*) this ->getChildByTag(923);
label->setString(CCString::createWithFormat("内部抬起")->getCString());
}
// 按钮在其外部抬起事件回调
void HelloWorld::touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent) {
CCLabelTTF * label = (CCLabelTTF*) this ->getChildByTag(923);
label->setString(CCString::createWithFormat("外部抬起")->getCString());
}
==========================.h文件
<pre name="code" class="cpp">.h文件
#include "cocos2d.h" #include "cocos-ext.h"
using namespace cocos2d;
using namespace extension;
class HelloWorld : public cocos2d::CCLayer {
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
CREATE_FUNC(HelloWorld);
// 按下button事件回调
void touchDownAction(CCObject * sender , CCControlEvent * controlEvent);
// button在其内部拾起事件回调
void touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent);
// button在其外部拾起事件回调
void touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent);
};
版权声明:本文博主原创文章,博客,未经同意不得转载。