小说网 找小说 无限小说 烟雨红尘 幻想小说 酷文学 深夜书屋

cocos2dx CCControlSlider


有的同学建议先上图,好吧,先上效果图


再看代码,创建了两个CCControlSlider在主窗口中

// on "init" you need to initialize your instance
bool ControlSlider01::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !MenuLayer::init() )
    {
        return false;
    }
    
    
    CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
    
    // Add the slider
    CCControlSlider *slider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
    slider->setAnchorPoint(ccp(0.5f, 1.0f));
    slider->setMinimumValue(0.0f); // Sets the min value of range
    slider->setMaximumValue(5.0f); // Sets the max value of range
    slider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f + 16));
    slider->setTag(1);
    
    // When the value of the slider will change, the given selector will be call
    slider->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlSlider01::valueChanged), CCControlEventValueChanged);
    
    CCControlSlider *restrictSlider = CCControlSlider::create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png");
    restrictSlider->setAnchorPoint(ccp(0.5f, 1.0f));
    restrictSlider->setMinimumValue(0.0f); // Sets the min value of range
    restrictSlider->setMaximumValue(5.0f); // Sets the max value of range
    restrictSlider->setMaximumAllowedValue(4.0f);
    restrictSlider->setMinimumAllowedValue(1.5f);
    restrictSlider->setValue(3.0f);
    restrictSlider->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f - 24));
    restrictSlider->setTag(2);
    
	//same with restricted
    restrictSlider->addTargetWithActionForControlEvents(this, cccontrol_selector(ControlSlider01::valueChanged), CCControlEventValueChanged);
    
    addChild(slider);
    addChild(restrictSlider);
    return true;

}

void ControlSlider01::valueChanged(CCObject* obj, CCControlEvent)
{
    CCControlSlider* slider = (CCControlSlider*)obj;
    float val = slider->getValue();
    CCLOG("%f", val);
}

简单的创建了两个CCControlSlider

posted on 2014-05-01 21:35  牛栏山1  阅读(86)  评论(0编辑  收藏  举报

导航