专注,努力,勤奋

专注,努力,勤奋

cocos2dx之创建Button

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

void MyBottonBastLayer::initLayer() {
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCScale9Sprite *psc9Selected = CCScale9Sprite::create("btn-about-selected.png");
    CCLabelTTF *label = CCLabelTTF::create("My Button", "Marker Felt", 30);
    CCScale9Sprite *psc9ButtonBG = CCScale9Sprite::create("extensions/buttonHighlighted.png");
    CCControlButton *button = CCControlButton::create(label, psc9ButtonBG);

    /*set the button is pressed state*/
    button->setPosition(ccp(500.0f, size.height / 3.0f));

    button->setBackgroundSpriteForState(psc9Selected, CCControlStateSelected);
    button->setTitleColorForState(ccWHITE, CCControlStateHighlighted);


    button->addTargetWithActionForControlEvents(this, cccontrol_selector(MyBottonBastLayer::buttonCallBack), CCControlEventTouchDown);
    this->addChild(button, 1);
}

其中:

    button->addTargetWithActionForControlEvents(this, cccontrol_selector(MyBottonBastLayer::buttonCallBack), CCControlEventTouchDown);

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

void MyBottonBastLayer::buttonCallBack(CCObject *object, CCControlEvent controlEvent) {
    CCLOG("Touch the Button");
}


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

/* 当鼠标处于按下并曾经点中按钮时,则触发一次 */  
    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);  


阅读更多
个人分类: C/C++ cocos2dx学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭