cocos2dx CCControlButton 按钮事件

2015年02月06日 18:52 0 点赞 0 评论 更新于 2017-05-08 14:46

在Cocos2d-x开发中,CCControlButton 是一个常用的控件,用于创建具有交互功能的按钮。本文将分享如何使用 CCControlButton 处理按钮事件,并提供相应的代码示例,希望对大家有所帮助。

代码示例

头文件部分

#include "cocos2d.h"
#include "cocos-ext.h"

using namespace cocos2d;
using namespace extension;

class HelloWorld : public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);

// 按下按钮事件回调
void touchDownAction(CCObject * sender, CCControlEvent * controlEvent);
// 按钮在其内部抬起事件回调
void touchUpInsideAction(CCObject * sender, CCControlEvent * controlEvent);
// 按钮在其外部抬起事件回调
void touchUpOutsideAction(CCObject * sender, CCControlEvent * controlEvent);
};

实现文件部分

bool HelloWorld::init()
{
// 1. super init first
if (!CCLayer::init())
{
return false;
}

// 创建按钮文字标签
CCLabelTTF* label = CCLabelTTF::create("未选中文字", "MarkerFelt", 25);
// 创建按钮,使用CCScale9Sprite作为背景
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());
}

代码解释

  1. 头文件包含:包含 cocos2d.hcocos-ext.h 头文件,引入必要的命名空间。
  2. HelloWorld 类定义:定义了 HelloWorld 类,继承自 CCLayer,包含 init 方法用于初始化,以及处理按钮事件的回调函数。
  3. init 方法
    • 首先调用父类的 init 方法进行初始化。
    • 创建按钮和文字标签,设置按钮的位置、选中状态下的文字颜色和内容。
    • 为按钮添加不同的事件回调函数,分别处理按下、内部抬起和外部抬起事件。
    • 创建一个用于显示按钮状态的标签,并添加到当前层。
  4. 事件回调函数touchDownActiontouchUpInsideActiontouchUpOutsideAction 分别处理按钮的不同事件,通过修改标签的文字内容来显示按钮的状态。

通过以上代码,你可以在Cocos2d-x项目中使用 CCControlButton 处理按钮事件,实现交互功能。

作者信息

boke

boke

共发布了 1025 篇文章