CCControlButton 事件

利用CCControlButton创建一个Button,代码如下:

[cpp]  view plain copy
  1. void MyBottonBastLayer::initLayer() {  
  2.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  3.     CCScale9Sprite *psc9Selected = CCScale9Sprite::create("btn-about-selected.png");  
  4.     CCLabelTTF *label = CCLabelTTF::create("My Button""Marker Felt", 30);  
  5.     CCScale9Sprite *psc9ButtonBG = CCScale9Sprite::create("extensions/buttonHighlighted.png");  
  6.     CCControlButton *button = CCControlButton::create(label, psc9ButtonBG);  
  7.   
  8.     /*set the button is pressed state*/  
  9.     button->setPosition(ccp(500.0f, size.height / 3.0f));  
  10.   
  11.     button->setBackgroundSpriteForState(psc9Selected, CCControlStateSelected);  
  12.     button->setTitleColorForState(ccWHITE, CCControlStateHighlighted);  
  13.   
  14.   
  15.     button->addTargetWithActionForControlEvents(this, cccontrol_selector(MyBottonBastLayer::buttonCallBack), CCControlEventTouchDown);  
  16.     this->addChild(button, 1);  
  17. }  

其中:

[cpp]  view plain copy
  1. button->addTargetWithActionForControlEvents(this, cccontrol_selector(MyBottonBastLayer::buttonCallBack), CCControlEventTouchDown);  

是button所点击的时候产生的行为:

[cpp]  view plain copy
  1. void MyBottonBastLayer::buttonCallBack(CCObject *object, CCControlEvent controlEvent) {  
  2.     CCLOG("Touch the Button");  
  3. }  


关于点击时,详细参数如下:

[cpp]  view plain copy
  1. /* 当鼠标处于按下并曾经点中按钮时,则触发一次 */    
  2.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);    
  3.     
  4.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发一次 */    
  5.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragEnter), CCControlEventTouchDragEnter);    
  6.     
  7.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发一次 */    
  8.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragExit), CCControlEventTouchDragExit);    
  9.     
  10.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标进入按钮范围,则触发,只要达到条件,就不断触发 */    
  11.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragInside), CCControlEventTouchDragInside);    
  12.     
  13.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标离开按钮范围,则触发,只要达到条件,就不断触发 */    
  14.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragOutside), CCControlEventTouchDragOutside);    
  15.     
  16.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围内,则触发一次 */    
  17.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInside), CCControlEventTouchUpInside);    
  18.     
  19.     /* 当鼠标处于按下并曾经点中按钮的状态下,鼠标松开且在按钮范围外,则触发一次 */    
  20.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutside), CCControlEventTouchUpOutside);    
  21.     
  22.     /* 暂时没有发现能用鼠标触发这个事件的操作,看了注释,应该是由其它事件中断按钮事件而触发的 */    
  23.     controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchCancel), CCControlEventTouchCancel); 

你可能感兴趣的:(CCControlButton 事件)