cocos2d-x Slider的使用



Size size = Director::getInstance()->getVisibleSize();
 cocos2d::ui::Button* button = Button::create("animationbuttonnormal.png","animationbuttonpressed.png");
 button->setPosition(Vec2(size.width / 2,size.height / 2));
 button->setPressedActionEnabled(true);
 button->addClickEventListener([=](Ref* sender){ //监听的一中方式
  CCLOG("Button clicked, position = (%f, %f)", button->getPosition().x,
   button->getPosition().y);
 });
 button->setName("button");
 this->addChild(button);
 button->setZoomScale(-0.5);

 cocos2d::ui::Slider* slider = cocos2d::ui::Slider::create();
 slider->loadBarTexture("sliderTrack.png");
 slider->loadSlidBallTextures("sliderThumb.png","sliderThumb.png","");
 slider->loadProgressBarTexture("sliderProgress.png");
 slider->setPosition(Vec2(size.width / 2.0f,size.height / 2 - 50));
 slider->addEventListener(CC_CALLBACK_2(HelloWorld::sliderEvent,this));
 slider->setPercent(button->getZoomScale()* 100);
 this->addChild(slider);


void HelloWorld::sliderEvent(Ref *pSender, cocos2d::ui::Slider::EventType type)
{
 if (type == cocos2d::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
 {
  cocos2d::ui::Slider* slider = dynamic_cast(pSender);
  int percent = slider->getPercent();
  log("------->%d", percent);
  Button* btn = (Button*)this->getChildByName("button");
  float zoomScale = percent * 0.01;

 }
}

你可能感兴趣的:(cocos2d-x Slider的使用)