Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating sliders


In this recipe, we will explain the slider. The slider will be used for tasks such as changing the volume of the sound or music. Cocos2d-x has a Slider class for it. If we use this class, we can create a slider easily.

Getting ready

So, let's prepare the images of the slider before we start. Please add these images in the Resouces/res folder.

  • sliderTrack.png: Background of the slider

  • sliderThumb.png: Image to move the slider

How to do it...

Let's create a slider by using the Slider class. First, you will generate a slider instance by using the sliderTrack.png image and the sliderThumb.png image. You will also specify the callback function as a lambda expression by using the addEventListener method when the slider value is changed.

auto slider = ui::Slider::create("res/sliderTrack.png",
"res/sliderThumb.png");
slider->setPosition(size/2);
this->addChild(slider);
slider->addEventListener([](Ref* sender, ui::Slider::EventType
type){
    auto slider = dynamic_cast<ui...