CheckBox是原生控件没有提供的
CheckBox
*
CheckBox
::create(
const
std
::
string
& backGround,
const std :: string & backGroundSeleted,
const std :: string & cross,
const std :: string & backGroundDisabled,
const std :: string & frontCrossDisabled,
const std :: string & backGroundSeleted,
const std :: string & cross,
const std :: string & backGroundDisabled,
const std :: string & frontCrossDisabled,
TextureResType texType)
backGround 普通状态下图片
backGroundSeleted
普通状态下被点击时图片
cross
激活状态下图片
backGroundDisabled
正常状态下不可用时图片
frontCrossDisabled
激活状态下被点击时图片
texType 纹理资源类型
void
CheckBox
::addEventListenerCheckBox(
Ref
*target,
SEL_SelectedStateEvent
selector)
{
_checkBoxEventListener = target;
_checkBoxEventSelector = selector;
}
void CheckBox ::addEventListener( const ccCheckBoxCallback & callback)
{
_checkBoxEventCallback = callback;
{
_checkBoxEventListener = target;
_checkBoxEventSelector = selector;
}
void CheckBox ::addEventListener( const ccCheckBoxCallback & callback)
{
_checkBoxEventCallback = callback;
}
代码:
#include
"ui/CocosGUI.h"
//******
using
namespace
cocos2d
::
ui
;
void
selectedEvent(
Ref
* pSender,
CheckBox
::
EventType
type);
private
:
Size size;
CheckBox* checkBox1;
Size size;
CheckBox* checkBox1;
CheckBox* checkBox2;
#include
"ui/CocosGUI.h"
//******
using
namespace
cocos2d
::
ui
;
size
=
Director
::
getInstance
()->
getWinSize
();
checkBox1
=
CheckBox
::
create
();
checkBox1 -> setTouchEnabled ( true );
///cocos2d-x-3.3/tests/cpp-tests/Resources/ccs-res/cocosui/check_box_active_press.png
checkBox1 -> loadTextures ( "check_box_normal.png" ,
"check_box_normal_press.png" ,
"check_box_active.png" ,
"check_box_normal_disable.png" ,
"check_box_active_disable.png" );
checkBox1 -> setPosition ( Vec2 ( size . width * 0.3 , size . height * 0.5 ));
// 添加监听事件
checkBox1 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
addChild ( checkBox1 );
// 设置初始状态为选择状态
checkBox1 -> setSelected ( true );
checkBox1 -> setTag ( 1 );
// 添加文本
auto label1 = Text :: create ();
label1-> setFontSize ( 20 );
label1-> setPosition ( Vec2 ( size . width * 0.38 , size . height * 0.5 ));
label1-> setString ( "Male" );
addChild (label1);
checkBox1 -> setTouchEnabled ( true );
///cocos2d-x-3.3/tests/cpp-tests/Resources/ccs-res/cocosui/check_box_active_press.png
checkBox1 -> loadTextures ( "check_box_normal.png" ,
"check_box_normal_press.png" ,
"check_box_active.png" ,
"check_box_normal_disable.png" ,
"check_box_active_disable.png" );
checkBox1 -> setPosition ( Vec2 ( size . width * 0.3 , size . height * 0.5 ));
// 添加监听事件
checkBox1 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
addChild ( checkBox1 );
// 设置初始状态为选择状态
checkBox1 -> setSelected ( true );
checkBox1 -> setTag ( 1 );
// 添加文本
auto label1 = Text :: create ();
label1-> setFontSize ( 20 );
label1-> setPosition ( Vec2 ( size . width * 0.38 , size . height * 0.5 ));
label1-> setString ( "Male" );
addChild (label1);
//
添加
Female
单选框
checkBox2 = CheckBox :: create ();
checkBox2 -> setTouchEnabled ( true );
checkBox2 -> loadTextures ( "check_box_normal.png" ,
"check_box_normal_press.png" ,
"check_box_active.png" ,
"check_box_normal_disable.png" ,
"check_box_active_disable.png" );
checkBox2 -> setPosition ( Vec2 ( size . width * 0.6 , size . height * 0.5 ));
checkBox2 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
addChild ( checkBox2 );
checkBox2 -> setTag ( 2 );
// 添加文本
auto label2 = Text :: create ();
label2-> setFontSize ( 20 );
label2-> setPosition ( Vec2 ( size . width * 0.7 , size . height * 0.5 ));
label2-> setString ( "Female" );
addChild (label2);
checkBox2 = CheckBox :: create ();
checkBox2 -> setTouchEnabled ( true );
checkBox2 -> loadTextures ( "check_box_normal.png" ,
"check_box_normal_press.png" ,
"check_box_active.png" ,
"check_box_normal_disable.png" ,
"check_box_active_disable.png" );
checkBox2 -> setPosition ( Vec2 ( size . width * 0.6 , size . height * 0.5 ));
checkBox2 -> addEventListener ( CC_CALLBACK_2 ( HelloWorld :: selectedEvent , this ));
addChild ( checkBox2 );
checkBox2 -> setTag ( 2 );
// 添加文本
auto label2 = Text :: create ();
label2-> setFontSize ( 20 );
label2-> setPosition ( Vec2 ( size . width * 0.7 , size . height * 0.5 ));
label2-> setString ( "Female" );
addChild (label2);
void
HelloWorld
::selectedEvent(
Ref
* pSender,
CheckBox
::
EventType
type){
auto checkBox = ( CheckBox *)pSender;
int nTag =checkBox-> getTag ();
switch (type) {
case CheckBox :: EventType :: UNSELECTED :
if (nTag== 1 ) {
checkBox2 -> setSelected ( true );
CCLOG ( "-------1-------" );
} else {
checkBox1 -> setSelected ( true );
CCLOG ( "-------2-------" );
}
break ;
case CheckBox :: EventType :: SELECTED :
if (nTag== 1 ) {
checkBox2 -> setSelected ( false );
CCLOG ( "-------3-------" );
} else {
checkBox1 -> setSelected ( false );
CCLOG ( "-------4-------" );
}
break ;
}
auto checkBox = ( CheckBox *)pSender;
int nTag =checkBox-> getTag ();
switch (type) {
case CheckBox :: EventType :: UNSELECTED :
if (nTag== 1 ) {
checkBox2 -> setSelected ( true );
CCLOG ( "-------1-------" );
} else {
checkBox1 -> setSelected ( true );
CCLOG ( "-------2-------" );
}
break ;
case CheckBox :: EventType :: SELECTED :
if (nTag== 1 ) {
checkBox2 -> setSelected ( false );
CCLOG ( "-------3-------" );
} else {
checkBox1 -> setSelected ( false );
CCLOG ( "-------4-------" );
}
break ;
}
}