CCControlSwitch 、CCControlSlider、CCControlButton

时间:2023-03-10 03:07:49
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",),
CCLabelTTF::create("Off","Arial-BoldMT",)
);
pSwitch->setPosition(ccp(,));
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(,));
slider->setMaximumValue();
slider->setMinimumValue();
addChild(slider,,);
slider->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::changeValue),CCControlEventValueChanged); CCLabelTTF* ttf = CCLabelTTF::create("","Helvetica",);
ttf->setPosition(ccp(,));
ttf->setString(CCString::createWithFormat("current value = %.02f", slider->getValue())->getCString());
addChild(ttf,,); void HelloWorld::changeValue(CCObject *sender, CCControlEvent controlEvent)
{
CCControlSlider* slider = (CCControlSlider*)this->getChildByTag();
CCLabelTTF* ttf = (CCLabelTTF*)getChildByTag();
ttf->setString(CCString::createWithFormat("current value = %.02f", slider->getValue())->getCString());
}
//init()
CCLabelTTF *titleButton = CCLabelTTF::create("NO", "Marker Felt", );
CCControlButton * btn = CCControlButton::create(titleButton,CCScale9Sprite::create("button.png"));
btn->setPosition(ccp(,)); //按钮被选中后背景图响应的状态
btn->setBackgroundSpriteForState(CCScale9Sprite::create("buttonHighlighted.png"), CCControlStateHighlighted);
//按钮被选中后文字颜色响应的状态
btn->setTitleColorForState(ccc3(, , ), 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", );
addChild(titleButtonState,,);
titleButtonState->setPosition(ccp(,)); 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();
m_pDisplayValueLabel->setString(CCString::createWithFormat("Push")->getCString());
}
void HelloWorld::touchUpInsideAction(CCObject *sender, CCControlEvent controlEvent)
{
CCLabelTTF *m_pDisplayValueLabel = (CCLabelTTF*)this->getChildByTag();
m_pDisplayValueLabel->setString(CCString::createWithFormat("Inner Up")->getCString());
}
void HelloWorld::touchUpOutsideAction(CCObject *sender, CCControlEvent controlEvent){
CCLabelTTF *m_pDisplayValueLabel = (CCLabelTTF*)this->getChildByTag();
m_pDisplayValueLabel->setString(CCString::createWithFormat("Outer Up")->getCString());
}