CCControlSwitch 、CCControlSlider、CCControlButton

/*
        *bool hasMoved(); 这里获取的不是开关是否正在被用户拨动,而是开关最终的状态是由用户手动拨动开关进行的,
        *还是用户点击开关进行的状态更改
        */

        CCControlSwitch* pSwitch = CCControlSwitch::create(
            CCSprite::create("switch-mask.png"),
            CCSprite::create("switch-on.png"),
            CCSprite::create("switch-off.png"),
            CCSprite::create("switch-thumb.png"),
            CCLabelTTF::create("On","Arial-BoldMT",16),
            CCLabelTTF::create("Off","Arial-BoldMT",16)
            );
        pSwitch->setPosition(ccp(200,200));
        pSwitch->setOn(true);
        pSwitch->setEnabled(true);
        CCLog("是否打开状态:%i", pSwitch->isOn());
        CCLog("是否手动拖动的开关:%i", pSwitch->hasMoved());
        addChild(pSwitch);
CCControlSlider* slider = CCControlSlider::create(
            "sliderTrack.png","sliderProgress.png","sliderThumb.png");
        slider->setPosition(ccp(200,200));
        slider->setMaximumValue(100);
        slider->setMinimumValue(0);
        addChild(slider,0,888);
        slider->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::changeValue),CCControlEventValueChanged);

        CCLabelTTF* ttf = CCLabelTTF::create("","Helvetica",20);
        ttf->setPosition(ccp(200,220));
        ttf->setString(CCString::createWithFormat("current value = %.02f", slider->getValue())->getCString());
        addChild(ttf,0,999);




void HelloWorld::changeValue(CCObject *sender, CCControlEvent controlEvent)
{
    CCControlSlider* slider = (CCControlSlider*)this->getChildByTag(888);
    CCLabelTTF* ttf = (CCLabelTTF*)getChildByTag(999);
    ttf->setString(CCString::createWithFormat("current value = %.02f", slider->getValue())->getCString());
}
//init()
CCLabelTTF *titleButton = CCLabelTTF::create("NO", "Marker Felt", 25);
        CCControlButton * btn  = CCControlButton::create(titleButton,CCScale9Sprite::create("button.png"));
        btn->setPosition(ccp(240,170));

        //按钮被选中后背景图响应的状态
        btn->setBackgroundSpriteForState(CCScale9Sprite::create("buttonHighlighted.png"), CCControlStateHighlighted);
        //按钮被选中后文字颜色响应的状态
        btn->setTitleColorForState(ccc3(255, 0, 0), CCControlStateHighlighted);
        //按钮被选中后文字响应的状态
        btn->setTitleForState(CCString::create("YES"), CCControlStateHighlighted);
        addChild(btn);

        //按钮按下事件回调
        btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);
        //按钮在其内部抬起事件回调
        btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInsideAction), CCControlEventTouchUpInside);
        //按钮在其外部抬起事件回调
        btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutsideAction), CCControlEventTouchUpOutside);

        //用于显示按钮状态
        CCLabelTTF *titleButtonState = CCLabelTTF::create("", "Marker Felt", 25);
        addChild(titleButtonState,0,923);
        titleButtonState->setPosition(ccp(240,220));




void touchDownAction(CCObject* sender, CCControlEvent controlEvent);
    void touchUpInsideAction(CCObject* sender, CCControlEvent controlEvent);
    void touchUpOutsideAction(CCObject* sender, CCControlEvent controlEvent);

void HelloWorld::touchDownAction(CCObject *senderz, CCControlEvent controlEvent)
{
    CCLabelTTF *m_pDisplayValueLabel = (CCLabelTTF*)this->getChildByTag(923);
    m_pDisplayValueLabel->setString(CCString::createWithFormat("Push")->getCString());
}
void HelloWorld::touchUpInsideAction(CCObject *sender, CCControlEvent controlEvent)
{
    CCLabelTTF *m_pDisplayValueLabel = (CCLabelTTF*)this->getChildByTag(923);
    m_pDisplayValueLabel->setString(CCString::createWithFormat("Inner Up")->getCString());
}
void HelloWorld::touchUpOutsideAction(CCObject *sender, CCControlEvent controlEvent){
    CCLabelTTF *m_pDisplayValueLabel = (CCLabelTTF*)this->getChildByTag(923);
    m_pDisplayValueLabel->setString(CCString::createWithFormat("Outer Up")->getCString());
}
原文地址:https://www.cnblogs.com/MrGreen/p/3444698.html