9 cocos2d-x CCControlSlider (滑动条)

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    // 滑动条
    cocos2d::extension::CCControlSlider *slider=cocos2d::extension::CCControlSlider::create("abc.png", "abc3.png", "Icon.png");
    slider->setPosition(ccp(200, 170));
    slider->setMaximumValue(100);
    slider->setMinimumValue(0);
    this->addChild(slider,1,192);
    //设置监听,当滑动条的值发生变化后,会响应valueChanged函数
    slider->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::valudeChangedMethod), cocos2d::extension::CCControlEventValueChanged);
    
    //用于显示滑动条的值
    CCLabelTTF *label=CCLabelTTF::create("", "Helvetica", 20);
    label->setPosition(ccp(200, 210));
    
    label->setString(CCString::createWithFormat("滑动条当前值:%.02f",slider->getValue())->getCString());
    this->addChild(label,0,999);
    
    return true;
}

void HelloWorld::valudeChangedMethod(CCObject *pSender)
{
    cocos2d::extension::CCControlSlider *slider=(cocos2d::extension::CCControlSlider*)this->getChildByTag(192);
    CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(999);
    label->setString(CCString::createWithFormat("滑动条当前值:%.02f",slider->getValue())->getCString());
    
}

你可能感兴趣的:(9 cocos2d-x CCControlSlider (滑动条))