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. java基础18 String字符串和Object类(以及“equals” 和 “==”的解析)

    一.String字符串 问:笔试题:new String("abc")创建了几个对象?答:两个对象,一个对象是 位于堆内存,一个对象位于字符串常量池 class Demo17 { ...

  2. oracle一些笔记

    1.字符串类型字段 区分大小写 where table_name = 'MIDDLE' 2.execute immediate '' bulk collect into v_xxx_tab 3.列别名 ...

  3. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  4. 20165203迭代和JDB测试

    1.使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 public class C { public static void main(Strin ...

  5. mongodb优化篇

    在掌握了mongo的体系结构和基本操作后,开始学习 mongodb的优化,由于资源有限,只能网络上整理一些资料,我大致理解的mongo的优化分为以下几步:  1.监控 mongodb可以通过profi ...

  6. 【LOJ】#2123. 「HEOI2015」最短不公共子串

    题解 我们对于B串建出后缀自动机和序列自动机 对于问题1,枚举左端点然后跑后缀自动机,直到不能匹配作为这个左端点的答案 对于问题2,枚举左端点然后跑序列自动机,直到不能匹配 对于问题3,设f[i][j ...

  7. hadoop集群的搭建(分布式安装)

    集群 计算机集群是一种计算机系统,他通过一组松散集成的计算机软件和硬件连接起来高度紧密地协同完成计算工作. 集群系统中的单个计算机通常称为节点,通常通过局域网连接. 集群技术的特点: 1.通过多台计算 ...

  8. Eclipse反编译插件的安装

    步骤: 1.已经安装了Eclipse,如我的Eclipse目录:C:\Programs\JAVA\eclipse 2.反编译插件包:eclipse 反编译插件 jad 3.3.0.zip 3.解压反编 ...

  9. 20169211《Linux内核原理与分析》 第十周作业

    一.Linux内核之进程地址空间学习总结 Linux内核除了要管理物理内存还需要管理虚拟内存.用户进程的地址空间就是虚拟内存的一部分.每个用户进程都独有一个地址空间.由于是虚拟化的内存,所以从每个进程 ...

  10. TI科学家谈浮点DSP未来发展

        自十多年前浮点数字信号处理器(DSP)诞生以来,便为实时信号处理提供了算术上更为先进的备选方案.不过,定点器件至今仍是业界的主流.当然低成本是主要原因.定点DSP每器件产品的价格很低,这对大规 ...