ControlButton按钮的大小可以根据标签内容进行缩放,同时它具有很多按钮所需要的功能。
<span style="white-space:pre"> </span>//正常状态下的按钮图片 Scale9Sprite* btnNormal = Scale9Sprite::create("button.png"); //单击状态下的按钮图片 Scale9Sprite* btnPress = Scale9Sprite::create("buttonHighlighted.png"); //按钮标题 LabelTTF* title = LabelTTF::create("touch me !","Marker Felt",30); //创建按钮,按钮的大小根据标题自动调整 ControlButton* btn = ControlButton::create(title,btnNormal); //设置按钮按下时的图片 btn->setBackgroundSpriteForState(btnPress,Control::State::SELECTED); //强制设置按钮大小,如果按钮超过这个范围,则自动扩大 btn->setPreferredSize(Size(300,50)); btn->setPosition(<span style="font-family:Arial, Helvetica, sans-serif;">Point</span>(200,200)); this->addChild(btn);
State:NORMAL、HIGH_LIGHTED 、DISABLED、SELECTED
NORMAL = 1 << 0, // The normal, or default state of a control—that is, enabled but neither selected nor highlighted. HIGH_LIGHTED = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property. DISABLED = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property. SELECTED = 1 << 3 // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property.个人暂时不太了解这几个状态的用处,日后补充
playMusic.h
#ifndef __PLAYMUSIC_H__ #define __PLAYMUSIC_H__ #include "cocos2d.h" #include "extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class PlayMusic : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(PlayMusic); void addChildAt(Node *node, float percentageX, float percentageY); private: void touchDown(Ref* pSender,Control::EventType event); void dragInside(Ref* pSender,Control::EventType event); void dragOutside(Ref* pSender,Control::EventType event); void dragEnter(Ref* pSender,Control::EventType event); void dragExit(Ref* pSender,Control::EventType event); void touchUpInside(Ref* pSender,Control::EventType event); void touchUpOutside(Ref* pSender,Control::EventType event); void touchCancel(Ref* pSender,Control::EventType event); void valueChanged(Ref* pSender,Control::EventType event); }; #endif
#include "playMusic.h" #include "extensions/cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; Scene* PlayMusic::createScene() { auto scene = Scene::create(); auto layer = PlayMusic::create(); scene->addChild(layer); return scene; } bool PlayMusic::init() { if ( !Layer::init() ) { return false; } //正常状态下的按钮图片 Scale9Sprite* btnNormal = Scale9Sprite::create("button.png"); //单击状态下的按钮图片 Scale9Sprite* btnPress = Scale9Sprite::create("buttonHighlighted.png"); //按钮标题 LabelTTF* title = LabelTTF::create("touch me !","Marker Felt",30); //创建按钮,按钮的大小根据标题自动调整 ControlButton* btn = ControlButton::create(title,btnNormal); //设置按钮按下时的图片 btn->setBackgroundSpriteForState(btnPress,Control::State::HIGH_LIGHTED); //强制设置按钮大小,如果按钮超过这个范围,则自动扩大 btn->setPreferredSize(Size(300,50)); btn->setPosition(Point(200,200)); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchDown),Control::EventType::TOUCH_DOWN); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::dragInside),Control::EventType::DRAG_INSIDE); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::dragOutside ),Control::EventType::DRAG_OUTSIDE ); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::dragEnter),Control::EventType::DRAG_ENTER); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::dragExit),Control::EventType::DRAG_EXIT); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchUpInside),Control::EventType::TOUCH_UP_INSIDE); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchUpOutside ),Control::EventType::TOUCH_UP_OUTSIDE ); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::touchCancel),Control::EventType::TOUCH_CANCEL); btn->addTargetWithActionForControlEvents(this,cccontrol_selector(PlayMusic::valueChanged),Control::EventType::VALUE_CHANGED); this->addChild(btn); return true; } void PlayMusic::touchDown(Ref* pSender,Control::EventType event) { log("touchdown"); } void PlayMusic::dragInside(Ref* pSender,Control::EventType event) { log("dragInside"); } void PlayMusic::dragOutside(Ref* pSender,Control::EventType event) { log("dragOutside"); } void PlayMusic::dragEnter(Ref* pSender,Control::EventType event) { log("dragEnter"); } void PlayMusic::dragExit(Ref* pSender,Control::EventType event) { log("dragExit"); } void PlayMusic::touchUpInside(Ref* pSender,Control::EventType event) { log("touchUpInside"); } void PlayMusic::touchUpOutside(Ref* pSender,Control::EventType event) { log("touchUpOutside"); } void PlayMusic::touchCancel(Ref* pSender,Control::EventType event) { log("touchCancel"); } void PlayMusic::valueChanged(Ref* pSender,Control::EventType event) { log("valueChanged"); }
TOUCH_DOWN = 1 << 0, // A touch-down event in the control. DRAG_INSIDE = 1 << 1, // An event where a finger is dragged inside the bounds of the control. DRAG_OUTSIDE = 1 << 2, // An event where a finger is dragged just outside the bounds of the control. DRAG_ENTER = 1 << 3, // An event where a finger is dragged into the bounds of the control. DRAG_EXIT = 1 << 4, // An event where a finger is dragged from within a control to outside its bounds. TOUCH_UP_INSIDE = 1 << 5, // A touch-up event in the control where the finger is inside the bounds of the control. TOUCH_UP_OUTSIDE = 1 << 6, // A touch-up event in the control where the finger is outside the bounds of the control. TOUCH_CANCEL = 1 << 7, // A system event canceling the current touches for the control. VALUE_CHANGED = 1 << 8 // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.TOUCH_DOWN 按下按钮调用事件