• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
linux_ios
博客园    首页    新随笔    联系   管理    订阅  订阅

cocos2dx CCControlButton 按钮事件

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);
};

 

posted @ 2013-04-03 23:58  linux_ios  阅读(2248)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3