CCControlSlider 滑动条

新建工程,名为testSlider

修改HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

#include "cocos-ext.h"

using namespace cocos2d::extension;

class HelloWorld : public cocos2d::CCLayer

{

public:

    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

    virtual bool init();


    // there's no 'id' in cpp, so we recommend to return the class instance pointer

    static cocos2d::CCScene* scene();

    

    CREATE_FUNC(HelloWorld);

    void valueChange();

};


#endif // __HELLOWORLD_SCENE_H__

修改HelloWorldScene.cpp

#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;

    }


    CCControlSlider *slider=CCControlSlider::create("sliderBg.png", "sliderProgress.png", "sliderThumb.png");

    slider->setPosition(ccp(200, 170));

    

    //设置滑动条最大值

    slider->setMaximumValue(100);

    //设置滑动条最小值

    slider->setMinimumValue(0);

    addChild(slider,0,921);

    

    //设置监听,当滑动条的值发生变化后,会响应valueChanged函数

    //CCControlEventValueChanged值改变

    slider->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::valueChange), CCControlEventValueChanged);

    

    //用来展示当前滑动条的滑动值

    CCLabelTTF *ttf=CCLabelTTF::create("", "Helvetica", 20);

    ttf->setPosition(ccp(200, 210));

    ttf->setString(CCString::createWithFormat("滑动条当前值=%.02f",slider->getValue())->getCString());

    addChild(ttf,0,922);

    return true;

}


void HelloWorld::valueChange()

{

    CCControlSlider *slider=(CCControlSlider *)this->getChildByTag(921);

    CCLabelTTF *ttf=(CCLabelTTF *)this->getChildByTag(922);

    ttf->setString(CCString::createWithFormat("滑动条当前值= %.02f",slider->getValue())->getCString());

}



你可能感兴趣的:(CCControlSlider 滑动条)