1. ControlButton的介绍
ControlButton按钮的大小可以根据标签内容进行缩放,同时它具有很多按钮所需要的功能。
2. ControlButton的使用
- <spanstyle="white-space:pre"></span>//正常状态下的按钮图片
- Scale9Sprite*btnNormal=Scale9Sprite::create("button.png");
-
- //单击状态下的按钮图片
- Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png");
-
- //按钮标题
- LabelTTF*title=LabelTTF::create("touchme!","MarkerFelt",30);
- //创建按钮,按钮的大小根据标题自动调整
- ControlButton*btn=ControlButton::create(title,btnNormal);
- //设置按钮按下时的图片
- btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED);
- //强制设置按钮大小,如果按钮超过这个范围,则自动扩大
- btn->setPreferredSize(Size(300,50));
- >setPosition(spanstyle="font-family:Arial,sans-serif;">Point>(200,200));
- this->addChild(btn);
btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED);
State:NORMAL、HIGH_LIGHTED 、DISABLED、SELECTED
NORMAL=1<<0,//Thenormal,ordefaultstateofacontrol—thatis,enabledbutneitherselectednorhighlighted.
- HIGH_LIGHTED=11,//Highlightedstateofacontrol.Acontrolentersthisstatewhenatouchdown,draginsideordragenterisperformed.Youcanretrieveandsetthisvaluethroughthehighlightedproperty.
- DISABLED=12,//Disabledstateofacontrol.Thisstateindicatesthatthecontroliscurrentlydisabled.Youcanretrieveandsetthisvaluethroughtheenabledproperty.
- SELECTED=13//Selectedstateofacontrol.Thisstateindicatesthatthecontroliscurrentlyselected.Youcanretrieveandsetthisvaluethroughtheselectedproperty.
个人暂时不太了解这几个状态的用处,日后补充
3. 按钮事件
playMusic.h
#ifndef__PLAYMUSIC_H__
- #define__PLAYMUSIC_H__
- #include"cocos2d.h"
- #include"extensions/cocos-ext.h"
- USING_NS_CC;
- USING_NS_CC_EXT;
- classPlayMusic:publiccocos2d::Layer{
- public:
- staticcocos2d::Scene*createScene();
- virtualboolinit();
- CREATE_FUNC(PlayMusic);
- voidaddChildAt(Node*node,floatpercentageX,floatpercentageY);
- private:
- voidtouchDown(Ref*pSender,Control::EventTypeevent);
- voiddragInside(Ref*pSender,Control::EventTypeevent);
- 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"> };
- #endif
playMusic.cpp
#include"playMusic.h"
- #include"extensions/cocos-ext.h"
- USING_NS_CC;
- USING_NS_CC_EXT;
- Scene*PlayMusic::createScene()
- {
- autoscene=Scene::create();
- autolayer=PlayMusic::create();
- scene->addChild(layer);
- returnscene;
- }
- boolPlayMusic::init()
- if(!Layer::init())
- returnfalse;
- }
- //正常状态下的按钮图片
- Scale9Sprite*btnNormal=Scale9Sprite::create("button.png");
- //单击状态下的按钮图片
- Scale9Sprite*btnPress=Scale9Sprite::create("buttonHighlighted.png");
- //按钮标题
- LabelTTF*title=LabelTTF::create("touchme!",30);
- //创建按钮,按钮的大小根据标题自动调整
- ControlButton*btn=ControlButton::create(title,btnNormal);
- //设置按钮按下时的图片
- //强制设置按钮大小,如果按钮超过这个范围,则自动扩大
- >setPosition(Point(200,200));
- >addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchDown),Control::EventType::TOUCH_DOWN);
- this->addChild(btn);
- returntrue;
- voidPlayMusic::touchDown(Ref*pSender,Control::EventTypeevent)
- {
- log("touchdown");
- voidPlayMusic::dragInside(Ref*pSender,Control::EventTypeevent)
- log("dragInside");
- 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");
- 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");
- 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");
- 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");
- 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");
- 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");
- 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");
- }
TOUCH_DOWN=1 DRAG_INSIDE=1 DRAG_OUTSIDE=1 DRAG_ENTER=13,//Aneventwhereafingerisdraggedintotheboundsofthecontrol.
- DRAG_EXIT=14,//Aneventwhereafingerisdraggedfromwithinacontroltooutsideitsbounds.
- TOUCH_UP_INSIDE=15,//Atouch-upeventinthecontrolwherethefingerisinsidetheboundsofthecontrol.
- TOUCH_UP_OUTSIDE=16,//Atouch-upeventinthecontrolwherethefingerisoutsidetheboundsofthecontrol.
- TOUCH_CANCEL=17,//Asystemeventcancelingthecurrenttouchesforthecontrol.
- 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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|