cocos2dx 3.x ControlButton的认识

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x ControlButton的认识前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. ControlButton的介绍

ControlButton按钮的大小可以根据标签内容进行缩放,同时它具有很多按钮所需要的功能

2. ControlButton的使用

  1. <spanstyle="white-space:pre"></span>//正常状态下的按钮图片
  2. Scale9Sprite*btnNormal=Scale9Sprite::create("button.png");
  3. //单击状态下的按钮图片
  4. Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png");
  5. //按钮标题
  6. LabelTTF*title=LabelTTF::create("touchme!","MarkerFelt",30);
  7. //创建按钮,按钮的大小根据标题自动调整
  8. ControlButton*btn=ControlButton::create(title,btnNormal);
  9. //设置按钮按下时的图片
  10. btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED);
  11. //强制设置按钮大小,如果按钮超过这个范围,则自动扩大
  12. btn->setPreferredSize(Size(300,50));
  13. >setPosition(spanstyle="font-family:Arial,sans-serif;">Point>(200,200));
  14. this->addChild(btn);

btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED);

State:NORMAL、HIGH_LIGHTED 、DISABLED、SELECTED

    NORMAL=1<<0,//Thenormal,ordefaultstateofacontrol—thatis,enabledbutneitherselectednorhighlighted.
  1. HIGH_LIGHTED=11,//Highlightedstateofacontrol.Acontrolentersthisstatewhenatouchdown,draginsideordragenterisperformed.Youcanretrieveandsetthisvaluethroughthehighlightedproperty.
  2. DISABLED=12,//Disabledstateofacontrol.Thisstateindicatesthatthecontroliscurrentlydisabled.Youcanretrieveandsetthisvaluethroughtheenabledproperty.
  3. SELECTED=13//Selectedstateofacontrol.Thisstateindicatesthatthecontroliscurrentlyselected.Youcanretrieveandsetthisvaluethroughtheselectedproperty.
个人暂时不太了解这几个状态的用处,日后补充


3. 按钮事件

playMusic.h

    #ifndef__PLAYMUSIC_H__
  1. #define__PLAYMUSIC_H__
  2. #include"cocos2d.h"
  3. #include"extensions/cocos-ext.h"
  4. USING_NS_CC;
  5. USING_NS_CC_EXT;
  6. classPlayMusic:publiccocos2d::Layer{
  7. public:
  8. staticcocos2d::Scene*createScene();
  9. virtualboolinit();
  10. CREATE_FUNC(PlayMusic);
  11. voidaddChildAt(Node*node,floatpercentageX,floatpercentageY);
  12. private:
  13. voidtouchDown(Ref*pSender,Control::EventTypeevent);
  14. voiddragInside(Ref*pSender,Control::EventTypeevent);
  15. voiddragOutside(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voiddragEnter(Ref*pSender,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> voiddragExit(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voidtouchUpInside(Ref*pSender,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> voidtouchUpOutside(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> voidtouchCancel(Ref*pSender,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> voidvalueChanged(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> };
  16. #endif

playMusic.cpp

    #include"playMusic.h"
  1. #include"extensions/cocos-ext.h"
  2. USING_NS_CC;
  3. USING_NS_CC_EXT;
  4. Scene*PlayMusic::createScene()
  5. {
  6. autoscene=Scene::create();
  7. autolayer=PlayMusic::create();
  8. scene->addChild(layer);
  9. returnscene;
  10. }
  11. boolPlayMusic::init()
  12. if(!Layer::init())
  13. returnfalse;
  14. }
  15. //正常状态下的按钮图片
  16. Scale9Sprite*btnNormal=Scale9Sprite::create("button.png");
  17. //单击状态下的按钮图片
  18. Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png");
  19. //按钮标题
  20. LabelTTF*title=LabelTTF::create("touchme!",30);
  21. //创建按钮,按钮的大小根据标题自动调整
  22. ControlButton*btn=ControlButton::create(title,btnNormal);
  23. //设置按钮按下时的图片
  24. //强制设置按钮大小,如果按钮超过这个范围,则自动扩大
  25. >setPosition(Point(200,200));
  26. >addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchDown),Control::EventType::TOUCH_DOWN);
  27. this->addChild(btn);
  28. returntrue;
  29. voidPlayMusic::touchDown(Ref*pSender,Control::EventTypeevent)
  30. {
  31. log("touchdown");
  32. voidPlayMusic::dragInside(Ref*pSender,Control::EventTypeevent)
  33. log("dragInside");
  34. voidPlayMusic::dragOutside(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragOutside");
  35. voidPlayMusic::dragEnter(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragEnter");
  36. voidPlayMusic::dragExit(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("dragExit");
  37. voidPlayMusic::touchUpInside(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchUpInside");
  38. voidPlayMusic::touchUpOutside(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchUpOutside");
  39. voidPlayMusic::touchCancel(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("touchCancel");
  40. voidPlayMusic::valueChanged(Ref*pSender,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> log("valueChanged");
  41. }

    TOUCH_DOWN=1 DRAG_INSIDE=1 DRAG_OUTSIDE=1 DRAG_ENTER=13,//Aneventwhereafingerisdraggedintotheboundsofthecontrol.
  1. DRAG_EXIT=14,//Aneventwhereafingerisdraggedfromwithinacontroltooutsideitsbounds.
  2. TOUCH_UP_INSIDE=15,//Atouch-upeventinthecontrolwherethefingerisinsidetheboundsofthecontrol.
  3. TOUCH_UP_OUTSIDE=16,//Atouch-upeventinthecontrolwherethefingerisoutsidetheboundsofthecontrol.
  4. TOUCH_CANCEL=17,//Asystemeventcancelingthecurrenttouchesforthecontrol.
  5. VALUE_CHANGED=18//Atouchdraggingorotherwisemanipulatingacontrol,causingittoemitaseriesofdifferentvalues.
TOUCH_DOWN 按下按钮调用事件
DRAG_INSIDE 当处于按下并点中按钮的状态下,进入按钮范围,则触发,可以多次触发
DRAG_OUTSIDE 当处于按下并点中按钮的状态下,离开按钮范围,则触发,可以多次触发
DRAG_ENTER 当处于按下并点中按钮的状态下,进入按钮范围,则触发,一次
DRAG_EXIT 当处于按下并点中按钮的状态下,离开按钮范围,则触发,一次
TOUCH_UP_INSIDE 当处于按下并点中按钮的状态下,鼠标松开且在按钮范围内,则触发,一次
TOUCH_UP_OUTSIDE 当处于按下并点中按钮的状态下,鼠标松开且在按钮范围外,则触发,一次
TOUCH_CANCEL 系统事件中断按钮事件而触发

VALUE_CHANGED 触摸拖曳或操纵控制,让它发出一系列的不同的值。


原文来自:http://blog.csdn.net/lengxue789/article/details/38228935

原文链接:https://www.f2er.com/cocos2dx/342097.html

猜你在找的Cocos2d-x相关文章