cocos2dx 3.x ControlButton的认识

1. ControlButton的介绍

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

2.  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);

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

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.
个人暂时不太了解这几个状态的用处,日后补充


3. 按钮事件

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

playMusic.cpp

#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            按下按钮调用事件
DRAG_INSIDE            当处于按下并点中按钮的状态下,进入按钮范围,则触发,可以多次触发
DRAG_OUTSIDE             当处于按下并点中按钮的状态下,离开按钮范围,则触发,可以多次触发
DRAG_ENTER             当处于按下并点中按钮的状态下,进入按钮范围,则触发,一次
DRAG_EXIT                     当处于按下并点中按钮的状态下,离开按钮范围,则触发,一次
TOUCH_UP_INSIDE     当处于按下并点中按钮的状态下,鼠标松开且在按钮范围内,则触发,一次
TOUCH_UP_OUTSIDE   当处于按下并点中按钮的状态下,鼠标松开且在按钮范围外,则触发,一次
TOUCH_CANCEL    系统事件中断按钮事件而触发
VALUE_CHANGED       触摸拖曳或操纵控制,让它发出一系列的不同的值。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值