Cocos2d—X游戏开发之CCToggle(菜单标签切换)

首先继承子CCMenu,是菜单标签中的一种。‘

class CC_DLL CCMenuItemToggle : public CCMenuItem
{
/** returns the selected item */
CC_PROPERTY(unsigned int, m_uSelectedIndex, SelectedIndex);
/** CCMutableArray that contains the subitems. You can add/remove items in runtime, and you can replace the array with a new one.
@since v0.7.2
*/
CC_PROPERTY(CCArray*, m_pSubItems, SubItems);
public:
CCMenuItemToggle()
: m_uSelectedIndex(0)
, m_pSubItems(NULL)
{}
virtual ~CCMenuItemToggle(); /** creates a menu item from a list of items with a target/selector */
static CCMenuItemToggle* createWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...); /** creates a menu item with no target/selector and no items */
static CCMenuItemToggle* create(); /** initializes a menu item from a list of items with a target selector */
bool initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args); /** creates a menu item with a item */
static CCMenuItemToggle* create(CCMenuItem *item); /** initializes a menu item with a item */
bool initWithItem(CCMenuItem *item);
/** add more menu item */
void addSubItem(CCMenuItem *item); /** return the selected item */
CCMenuItem* selectedItem();
// super methods
virtual void activate();
virtual void selected();
virtual void unselected();
virtual void setEnabled(bool var); virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
};

主要使用的下面的几个方法:

初始化菜单标签

static CCMenuItemToggle* createWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...);

返回当前有效的标签

CCMenuItem* selectedItem();

设置标签是否响应点击

virtual void setEnabled(bool var);

返回当前标签的数组下标

这个方法的声明是通过宏定义来实现的

CC_PROPERTY(unsigned int, m_uSelectedIndex, SelectedIndex);
#define CC_PROPERTY(varType, varName, funName)\
protected: varType varName;\
public: virtual varType get##funName(void);\
public: virtual void set##funName(varType var);
unsigned int CCMenuItemToggle::getSelectedIndex()
{
return m_uSelectedIndex;
}

有了这些方法,就可以实现菜单标签切换的效果了。

CCControlSwitch这个控件用于声音关闭,开启的选项。使用中要注意一点是,点的精灵一定要小。

class CCControlSwitchSprite;

/**
* @addtogroup GUI
* @{
* @addtogroup control_extension
* @{
*/ /** @class CCControlSwitch Switch control for Cocos2D. */
class CCControlSwitch : public CCControl
{
public:
CCControlSwitch();
virtual ~CCControlSwitch();
/** Initializes a switch with a mask sprite, on/off sprites for on/off states and a thumb sprite. */
bool initWithMaskSprite(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite); /** Creates a switch with a mask sprite, on/off sprites for on/off states and a thumb sprite. */
static CCControlSwitch* create(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite); /** Initializes a switch with a mask sprite, on/off sprites for on/off states, a thumb sprite and an on/off labels. */
bool initWithMaskSprite(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite, CCLabelTTF* onLabel, CCLabelTTF* offLabel); /** Creates a switch with a mask sprite, on/off sprites for on/off states, a thumb sprite and an on/off labels. */
static CCControlSwitch* create(CCSprite *maskSprite, CCSprite * onSprite, CCSprite * offSprite, CCSprite * thumbSprite, CCLabelTTF* onLabel, CCLabelTTF* offLabel); /**
* Set the state of the switch to On or Off, optionally animating the transition.
*
* @param isOn YES if the switch should be turned to the On position; NO if it
* should be turned to the Off position. If the switch is already in the
* designated position, nothing happens.
* @param animated YES to animate the "flipping" of the switch; otherwise NO.
*/
void setOn(bool isOn, bool animated);
void setOn(bool isOn);
bool isOn(void) { return m_bOn; }
bool hasMoved() { return m_bMoved; }
virtual void setEnabled(bool enabled); CCPoint locationFromTouch(CCTouch* touch);
//events
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); protected:
/** Sprite which represents the view. */
CCControlSwitchSprite* m_pSwitchSprite;
float m_fInitialTouchXPosition; bool m_bMoved;
/** A Boolean value that determines the off/on state of the switch. */
bool m_bOn;
};

CCControlSwitch的使用方法比较简单,看了上面的方法简介,应该使用没有问题,源码可以自己去研究

CCControlSwitch *switchMenu = CCControlSwitch::create(CCSprite::create("ZF_Shoot_RankingList_money1.png"), CCSprite::create("ZF_Shoot_RankingList_money1.png"), CCSprite::create("ZF_Shoot_RankingList_shoot1.png"), temp);
switchMenu->setPosition(ccp(winSize.width/2, winSize.height*0.5));
switchMenu->addTargetWithActionForControlEvents(this, cccontrol_selector(RankScene::menuCall), CCControlEventValueChanged);
this->addChild(switchMenu, 0);

然后,判断它的开关状态来调用相应的方法:

    CCControlSwitch *Sender = (CCControlSwitch *)pSender;
if (Sender->isOn())
{
CCLOG("************************************");
CCLOG("CCControlSwitch is on");
}
else
{
CCLOG("************************************");
CCLOG("CCControlSwitch is off");
}

Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)的更多相关文章

  1. Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决

    首先,先解决第一个问题,我们使用VS2010开发的时候,调试的时候,中文打印出来都是乱码,这个问题很纠结. 如下图: CCLOG("cclog: 测试使用标签的自动换行和个别字体大写&quo ...

  2. Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)

    CCScrollView在Cocos2d-X引擎中主要使用在图片尺寸远大于屏幕尺寸的时候使用. 总体来说,使用起来比较简单. 一个是CCScrollView控件本身,一个是CCScrollViewDe ...

  3. Cocos2d—X游戏开发之CCTableView详解(十一)

    本来很早就想写关于CCTableView的文章,但是在基本功能实现之后呢,项目需求增加导致对这个控件的研究必须更加深入一点. 好的,现在开始介绍一下这个控件,在Cocos2d—X引擎中,这是一个仿制i ...

  4. Cocos2d-x 3.x游戏开发之旅

    Cocos2d-x 3.x游戏开发之旅 钟迪龙 著   ISBN 978-7-121-24276-2 2014年10月出版 定价:79.00元 516页 16开 内容提要 <Cocos2d-x ...

  5. 【转载】浅谈游戏开发之2D手游工具

    浅谈游戏开发之2D手游工具 来源:http://www.gameres.com/459713.html 游戏程序 平台类型: iOS Android  程序设计: 其它  编程语言:   引擎/SDK ...

  6. [整理]Unity3D游戏开发之Lua

    原文1:[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上) 各位朋友,大家好,我是秦元培,欢迎大家关注我的博客,我地博客地址是blog.csdn.net/qinyuanpei.如果 ...

  7. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读

    ---------------------------------------------------------------------------------------------------- ...

  8. iOS游戏开发之UIDynamic

    iOS游戏开发之UIDynamic 简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 ...

  9. [Unity3D]Unity3D游戏开发之从Unity3D到Eclipse

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. 19 Error handling and Go go语言错误处理

    Error handling and Go go语言错误处理 12 July 2011 Introduction If you have written any Go code you have pr ...

  2. android设备休眠

    从上面的连接里面找到了一些资料: 如果一开始就对Android手机的硬件架构有一定的了解,设计出的应用程序通常不会成为待机电池杀手,而要设计出正确的通信机制与通信协议也并不困难.但如果不去了解而盲目设 ...

  3. js中的盒子模型

    说到盒子模型,你第一时间会想到css盒子模型,css中的盒子模型包括(内容区+内边距+边框).那在js中怎么去获取这些属性值呢?下面一起来学习js中的盒子模型. css样式 body { margin ...

  4. phpStudy配置https

    phpStudy配置https 1.打开vhosts-conf配置文件 2.在配置文件中增加如下内容 server { listen 443; server_name tam.gogugong.com ...

  5. 洛谷P1455搭配购买

    传送门啦 这是强连通分量与背包的例题 需要注意的就是价值和价格两个数组不要打反了.. 另外 这是双向图!!! #include <iostream> #include <cstdio ...

  6. 关于在调用JAVAFX相关包时遇到Access restriction: The type 'Application' is not API (restriction on required library)的解决方法

    点击工具栏的Project->Properties->Java Build Path->Libraries-> 双击第一项 点击Add添加允许javafx 然后就不会报错了

  7. winform框架源码-Devexpress开发框架

    链接: https://pan.baidu.com/s/1TnDj6qftGEUl3sTB8QXs_w 提取码: 关注公众号[GitHubCN]回复获取   开发模式:C/S C/S采用的是dev14 ...

  8. sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  9. 【LOJ】#2032. 「SDOI2016」游戏

    题解 看错题了,以为单次修改相当于一个覆盖,后来才明白"添加"-- 就相当于添加很多线段求最小值 首先这个等差数列添加的方式比较烦人,我们拆开两条链,一条s到lca,一条lca到t ...

  10. Python全栈开发之16、jquery

    一.查找元素 1.选择器 1.1 基本选择器      $("*")  $("#id")  $(".class")  $("ele ...