首页 > 代码库 > cocos2d-x3.0 Slider

cocos2d-x3.0 Slider


.h

#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
USING_NS_CC;

USING_NS_CC_EXT;
using namespace ui;


  
    RichText* _richText;
    Text* alert;


.cpp

   Size widgetSize = Director::getInstance()->getWinSize();
        
       
        
        
         layout = Layout::create();
        layout->setSize(Size(widgetSize.width, widgetSize.height));
       
        //横向排列,这里类似Android里的线性布局
        //layout->setLayoutType(LAYOUT_RELATIVE);
        /*以图片为背景*/
        layout->setBackGroundImageScale9Enabled(true);
        layout->setBackGroundImage("green_edit.png");
        
        layout->setPosition(Point(0,0));
        addChild(layout);
        
        
         alert = Text::create("Layout", "fonts/Marker Felt.ttf", 30 );
        alert->setColor(Color3B(159, 168, 176));
        alert->setPosition(Point(widgetSize.width / 2.0f,
                                 widgetSize.height / 2.0f - alert->getSize().height * 3.075f));
        
        layout->addChild(alert);

        /******************************************************************************************/
        Slider* slider = Slider::create();
        slider->loadBarTexture("sliderTrack.png");
        slider->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", "");
        slider->loadProgressBarTexture("slider_bar_active_9patch.png");
        slider->setScale9Enabled(true);
        slider->setCapInsets(Rect(0, 0, 0, 0));
        slider->setSize(Size(250.0, 19));
        
        slider->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
        slider->addEventListenerSlider(this, sliderpercentchangedselector(LayoutTest::sliderEvent));
        layout->addChild(slider);

void LayoutTest::sliderEvent(cocos2d::Ref *pSender, SliderEventType type)
{
    if (type == SLIDER_PERCENTCHANGED) {
        Slider* slider = dynamic_cast<Slider*>(pSender);
        int percent = slider->getPercent();
        alert->setText(StringUtils::format("Percent %d",percent).c_str());
        
    }
}