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. 洛谷P3088 挤奶牛

    传送门啦 这个题也是一个单调队列来优化的 $ dp $ ,我们考虑这个题,这个题让我们求出有多少奶牛会觉得拥挤,如果我们还像琪露诺那个题那样单纯用一次单调队列肯定是不行的,因为牛觉不觉得拥挤是受左右的 ...

  2. 每位架构师都应该熟知的 10 个 SOA 设计模式

    这 10 个 SOA 设计模式是如此之重要,其应用是如此之广泛,以至于它们都有些显而易见了. 1. 服务无关 服务无关实现对多种业务通用的逻辑.将服务无关的逻辑分离成离散的服务以方便服务的重用和整合. ...

  3. Kafka(五)Kafka的API操作和拦截器

    一 kafka的API操作 1.1 环境准备 1)在eclipse中创建一个java工程 2)在工程的根目录创建一个lib文件夹 3)解压kafka安装包,将安装包libs目录下的jar包拷贝到工程的 ...

  4. Spark(十二)SparkSQL简单使用

    一.SparkSQL的进化之路 1.0以前:   Shark 1.1.x开始:SparkSQL(只是测试性的)  SQL 1.3.x:          SparkSQL(正式版本)+Datafram ...

  5. 使用CSS3 @media 设置页面自适应

    参考CSS3 @media 查询 如果文档宽度小于 300 像素则修改背景演示(background-color): @media screen and (max-width: 300px) { bo ...

  6. USACO 6.1 Cow XOR

    Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...

  7. Session机制二(简易购物车案例)

    一:案例一(简易购物车) 1.目录结构 2.step1.jsp <%@ page language="java" contentType="text/html; c ...

  8. Ubuntu16.04下Kylin的安装与配置

    一.系统环境 kylin的安装配置并不像官方文档中描述的那样简单,复杂的原因在于hadoop,hive,hbase,kylin的版本一定要兼容,不然就会出现各种奇怪的错误.以下各软件版本可以成功运行k ...

  9. Ionic Js十一:模态

    ionicModal 可以遮住用户主界面的内容框. 你可以在你的 index 文件或者是其他文件内嵌入以下代码(里面的代码可以根据你自己的业务场景相应的改变). <script id=" ...

  10. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...