原文地址: http://blog.csdn.net/musicvs/article/details/8242963
按钮属性
按钮事件
回调函数
CCControlEvent 不是指针,千万不要习惯了就给它加一个*号
按钮属性
/* 正常状态下的按钮图片 */
CCScale9Sprite* btnNormal = CCScale9Sprite::create("button.png");
/* 点击状态下的按钮图片 */
CCScale9Sprite* btnDown = CCScale9Sprite::create("buttonHighlighted.png");
/* 按钮标题 */
CCLabelTTF *title = CCLabelTTF::create("Touch Me!3324234", "Marker Felt", 30);
/* 按钮的大小会根据标题自动调整 */
CCControlButton* controlBtn = CCControlButton::create(title, btnNormal);
/* 设置按钮按下时的图片 */
controlBtn->setBackgroundSpriteForState(btnDown, CCControlStateSelected);
controlBtn->setPosition(ccp(200, 200));
this->addChild(controlBtn);
/* 强制设置按钮大小,如果按钮标题大小超过这个范围,则会自动扩大 */
controlBtn->setPreferredSize(CCSize(300, 50));
按钮事件
/* 当鼠标处于按下并曾经点中按钮时,则触发一次 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragEnter), CCControlEventTouchDragEnter);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragExit), CCControlEventTouchDragExit);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragInside), CCControlEventTouchDragInside);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragOutside), CCControlEventTouchDragOutside);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInside), CCControlEventTouchUpInside);
/* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutside), CCControlEventTouchUpOutside);
/* 暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的 */
controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchCancel), CCControlEventTouchCancel);
回调函数
void touchDownAction(CCObject* pSender, CCControlEvent event);
void touchDragEnter(CCObject* pSender, CCControlEvent event);
void touchDragExit(CCObject* pSender, CCControlEvent event);
void touchDragInside(CCObject* pSender, CCControlEvent event);
void touchDragOutside(CCObject* pSender, CCControlEvent event);
void touchUpInside(CCObject* pSender, CCControlEvent event);
void touchUpOutside(CCObject* pSender, CCControlEvent event);
void touchCancel(CCObject* pSender, CCControlEvent event);
CCControlEvent 不是指针,千万不要习惯了就给它加一个*号