zoukankan      html  css  js  c++  java
  • cocos2d-x 2.2.6 之CCControlSwitch开关按钮

    感慨什么的不多说了,就是想创建声音,音效的开关按钮,可参考cocos2d-x的Demo代码,其文件为:

      CCControlSwitchTest.cpp。

    我将其资源放置到本人Demo的资源目录中,其大概代码如下:

    .h文件

    #include "cocos2d.h"
    #include "cocos-ext.h"
    
    USING_NS_CC;
    USING_NS_CC_EXT;
    
    // 声音音效的设定(仅写关键代码,且只写声音,其它的就不做编写了)
    class UISwitch : public CCLayer
    {
    public:
        virtual bool init();
        virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    
    private:
        void MusicChanged(CCObject* pSender,CCControlEvent controlEvent);   // 声音
    
    private:
        CCControlSwitch* m_pWavSwitch;      // 声音按钮
        bool             m_bIsMusicTouched;   // 是否触摸声音按钮
    };

    .cpp文件

    bool UISwitch::init()
    {
         if(!CCLayer::init())
             return false;
    
        // 声音
        m_pMusicSwitch = CCControlSwitch::create(
            CCSprite::create("ui/switch-mask.png"),       // 按钮背景图
            CCSprite::create("ui/switch-on.png"),         // 开状态下背景图片
            CCSprite::create("ui/switch-off.png"),        // 关状态背景图片
            CCSprite::create("ui/switch-thumb.png"),      // 开关标记图片
            CCLabelTTF::create("On", "Arial", 16),        // 开文字标签
            CCLabelTTF::create("Off", "Arial", 16));     // 关文字标签
    
        if(m_pMusicSwitch != NULL)
        {
            // 注册事件
            m_pMusicSwitch->addTargetWithActionForControlEvents(this,
                cccontrol_selector(UISwitch::MusicChanged),
                CCControlEventValueChanged);
         // 设定位置
            m_pMusicSwitch->setPosition(ccp(550, 370));
            // 设置状态为开启
            m_pMusicSwitch->setOn(true);            
            this->addChild(m_pMusicSwitch,6);
        }
    }
    
    void UISwitch::MusicChanged( CCObject* pSender,CCControlEvent controlEvent )
    {
        CCControlSwitch* pSwitch = (CCControlSwitch*)pSender;
        // 判断状态是否为开启
        if (pSwitch->isOn())
        {
            CCLog("UIPause music on");
        } 
        else
        {
            CCLog("UIPause music off");
        }
    }

    这些是按照cocos2d-x自带的demo来参考编写的,很不幸,事件点击无效,查看CCCcontrolSwitch类中发现,一个接口为:

    bool hasMoved() { return m_bMoved; }

    而针对于m_bMoved的设定为true时,仅仅在CCControlSwitch::ccTouchMoved(...)中进行了设定,所以,我采取了如下方式进行解决问题,这就是在代码中,我设定了bool m_bIsMusicTouched的原因,接下的代码如下:

    bool UISwitch::ccTouchBegan( CCTouch *pTouch, CCEvent *pEvent )
    {
        // 判断点击处是否为音乐开关按钮
        m_bIsMusicTouched = m_pMusicSwitch->ccTouchBegan(pTouch,pEvent);
    }
    
    void UISwitch::ccTouchMoved( CCTouch *pTouch, CCEvent *pEvent )
    {
        // 音乐按钮移动处理
        if(m_bIsMusicTouched)
            m_pMusicSwitch->ccTouchMoved(pTouch,pEvent);
    }
    
    void UISwitch::ccTouchEnded( CCTouch *pTouch, CCEvent *pEvent )
    {
        if(m_bIsMusicTouched)
        {
            m_pMusicSwitch->ccTouchEnded(pTouch,pEvent);
            m_bIsMusicTouched = false;
        }
    }

    这样的话,再点击就可以了,其效果如下:

    对了,大家要注意一个这样的东东,在bool init()中要添加这样的代码:

    // 参数一: 触摸接受的对象
    // 参数二: 优先级,值越小,优先级越高
    // 参数三: 是否“吞噬”触摸事件
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128,true);

    同时,在void onExit中添加如下代码:

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);

    不用了,就干掉,否则会引来其它触摸事件的问题,添加了这两句的主要目的就是为了防止事件透点,就说到这里吧。

    (本人新人,参考了网上很多师兄师姐的资料,但没有一味的复制粘贴而不做考证,如果依然出现了问题,希望大家能够指正,感谢!)

  • 相关阅读:
    博客地址
    node学习2
    正则表达式总结
    Git命令操作
    IDEA快捷键
    hibernate多对多(权限管理)
    所有国家的下拉框英文全称
    所有国家的下拉框英文简写
    所有国家的下拉框中文
    poj-1248 Safecracker
  • 原文地址:https://www.cnblogs.com/SkyflyBird/p/5174274.html
Copyright © 2011-2022 走看看