cocos2dx CCControlButton 按钮事件

http://bbs.9ria.com/thread-199317-1-1.html


  1. bool HelloWorld::init()
  2. {
  3.     //
  4.     // 1. super init first
  5.     if ( !CCLayer::init() )
  6.     {
  7.         return false;
  8.     }
  9.     CCLabelTTF * label = CCLabelTTF::create("为选中文字", "MarkerFelt",25);
  10.     CCControlButton * button = CCControlButton ::create(label,CCScale9Sprite::create("button.png"));
  11.     button->setPosition(ccp(240, 170));
  12. //    按钮选中后背景图响应的状态
  13.     button->setTitleColorForState(ccc3(255, 0, 0), CCControlStateHighlighted);
  14. //    按钮选中后文字响应的状态
  15.     button->setTitleForState(CCString::create("选中文字"), CCControlStateHighlighted);
  16.     
  17.     addChild(button);

  18.     //    按下按钮事件回调
  19.     button->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);
  20.     
  21.     //    按钮在其内部拾起事件回调
  22.     button->addTargetWithActionForControlEvents(this
  23.                                                 , cccontrol_selector( HelloWorld::touchUpInsideAction), CCControlEventTouchDragEnter);
  24.    //    按钮在其外部拾起事件回调
  25.     button->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchUpOutsideAction), CCControlEventTouchDragOutside);
  26.     
  27. //    用于显示按钮的状态
  28.     CCLabelTTF * la = CCLabelTTF ::create(" ", "MarkerFelt",20);
  29.     la->setColor(ccc3(255, 0, 0));
  30.     la->setPosition(ccp(240, 220));
  31.     addChild(la,0,923);

  32.     return true;
  33. }

  34. //    按下按钮事件回调
  35. void HelloWorld:: touchDownAction(CCObject * sender , CCControlEvent * controlEvent)
  36. {
  37.     CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);
  38.     label->setString(CCString::createWithFormat("按下")->getCString());

  39. }
  40. //    按钮在其内部抬起事件回调
  41. void HelloWorld::touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent)
  42. {
  43.     CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);
  44.     label->setString(CCString::createWithFormat("内部抬起")->getCString());
  45. }
  46. //    按钮在其外部抬起事件回调
  47. void HelloWorld::touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent)
  48. {
  49.     CCLabelTTF  * label = (CCLabelTTF*) this ->getChildByTag(923);
  50.     label->setString(CCString::createWithFormat("外部抬起")->getCString());

  51. }
复制代码



。cpp要声明的


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

  3. using namespace cocos2d;
  4. using namespace extension;

  5. class HelloWorld : public cocos2d::CCLayer
  6. {
  7. public:
  8.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
  9.     virtual bool init();

  10.     // there's no 'id' in cpp, so we recommend to return the class instance pointer
  11.     static cocos2d::CCScene* scene();
  12.     
  13.     // a selector callback
  14.     void menuCloseCallback(CCObject* pSender);

  15.     // preprocessor macro for "static create()" constructor ( node() deprecated )
  16.     CREATE_FUNC(HelloWorld);
  17.     
  18. //    按下按钮事件回调
  19.     void touchDownAction(CCObject * sender , CCControlEvent * controlEvent);
  20. //    按钮在其内部拾起事件回调
  21.     void touchUpInsideAction(CCObject * sender , CCControlEvent * controlEvent);
  22. //    按钮在其外部拾起事件回调
  23.         void touchUpOutsideAction(CCObject * sender , CCControlEvent * controlEvent);
  24. };
复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值