CCControlSlider 滑动条 新建工程,名为testSlider 修改HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __

 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());
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值