cocos2dx CCControlButton 按钮事件
2013-12-05 21:28
567 查看
.h文件
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
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());
}
.cpp文件
#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);
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());
}
.cpp文件
#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);
};
相关文章推荐
- sql2005 附加数据库出错(错误号:5123)解决方法
- PHP中return 和 exit 、break和contiue 区别与用法
- 浅析return false的正确使用
- js怎么终止程序return不行换jfslk
- sql2005 create file遇到操作系统错误5拒绝访问 错误1802
- MySQL里Create Index 能否创建主键 Primary Key
- 在数据库‘master’中拒绝CREATE DATABASE权限问题的解决方法
- 解析java中super的用法分析
- 解析php中die(),exit(),return的区别
- 解决MYSQL出现Can''t create/write to file ''#sql_5c0_0.MYD''的问题
- c语言中return与exit的区别浅析
- 浅析echo(),print(),print_r(),return之间的区别
- return false;和e.preventDefault();的区别
- CREATE FUNCTION sqlserver用户定义函数
- Create a TestDisk FreeDos LiveCD
- 迁移是10g-11g ogg正好有用武之地N种方法
- CSocket Create map_pp.cpp 179
- Ubuntu12.04 编译android源代码及生成模拟器经历分享
- 被调用函数向调用函数返回值