您的位置:首页 > 移动开发 > Cocos引擎

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

2013-08-07 15:16 489 查看
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");
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐