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. 06 Frequently Asked Questions (FAQ) 常见问题解答 (常见问题)

    Frequently Asked Questions (FAQ) Origins 起源 What is the purpose of the project? What is the history ...

  2. Python 常用的内建函数

    内建函数 ​ Build-in Function,启动python解释器,输入dir(__builtins__), 可以看到很多python解释器启动后默认加载的属性和函数,这些函数称之为内建函数, ...

  3. tf.metrics.accuracy ==>坑货

    tf.metrics.accuracy输出两个值,第一个值为上几步的平均精度,第二值是上几步与该步的精度的平均值. 正常的计算单个batch正确率的代码 self.correct_prediction ...

  4. 虚拟机 ubuntu 16.04

    下载地址:https://www.ubuntu.com/download/desktop 使用虚拟机直接安装

  5. jersey 过滤器名称绑定的问题 NameBinding Provider

    查资料也不容易查,这个问题困扰了我两天. 当没有 @Provider 的时候 过滤器不会被执行.

  6. TStringList 与 泛型字典TDictionary 的 哈希功能效率PK

    结论: 做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类. TDictionary类是一个name,value容 ...

  7. C实现线程池

    简介:这里使用linux下的互斥锁和条件变量实现了一个线程池.代码由一个未知作者完成,第二任作者补充优化. 本人仅仅是做了一些注释工作. 代码如下: /*! .h */ #include <st ...

  8. jQuery使用JSONP时的错误处理

    概述 什么是域,简单来说就是协议+域名或地址+端口,3者只要有任何一个不同就表示不在同一个域.跨域,就是在一个域中访问另一个域的数据. 如果只是加载另一个域的内容,而不需要访问其中的数据的话,跨域是很 ...

  9. 树莓派3B更换源为阿里源

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #科大源 sudo nano /etc/apt/sources.list deb htt ...

  10. MongoDB查询用法大全

    转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...