在CCScrollView中添加ccmenu实现滑动效果是不可能的,因为ccmenu的触发事件是你在touchBegan就全部捕获掉了,如果你想滑动CCScrollView取消选中这个菜单是无法实现的,.所以我们应该自己编写一个模拟ccmenu菜单的控件 ,在按下按钮后,如果不移动按钮的话,就触发触摸事件,移动的话就取消触摸事件,实现按钮在CCScrollView中的滚动效果。。。
- //
- // CCButtonSptite.h
- // CCSpriteButton
- //
- // Created by jasonsalex on 13-8-6.
- //
- //
- #ifndef __CCSpriteButton__CCButtonSptite__
- #define __CCSpriteButton__CCButtonSptite__
- #include "cocos2d.h"
- #define TOUCH_SENSITIVITY 20 //触摸灵敏度
- using namespace cocos2d;
- class ButtonSprite:public CCSprite,public CCTouchDelegate
- {
- public:
- ButtonSprite();
- virtual bool init(CCSpriteFrame *selFile,CCSpriteFrame *disFile);
- static ButtonSprite *create(const char *selFile,const char *disFile );
- static ButtonSprite *createWithSpriteFrame(CCSpriteFrame* selFrame,CCSpriteFrame *disFrame);
- static ButtonSprite *createWithSpriteFrameName(const char *selFile,const char * disFile);
- virtual ~ButtonSprite();
- virtual void onEnter();
- virtual void onExit();
- virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
- virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
- virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
- void registerScriptTapHandler(int nHandler);
- void unregisterScriptTapHandler(void);
- private:
- CCSprite *selSprite; //选择的精灵
- CCSprite *disSprite;//不选择的精灵
- int m_nScriptTapHandler;//脚本函数句柄
- bool isEmitTouchEvent; //是否发射触摸事件
- CCPoint startTouchPoint; //开始的触摸坐标
- };
- #endif /* defined(__CCSpriteButton__CCButtonSptite__) */
- //
- // CCButtonSptite.cpp
- // CCSpriteButton
- //
- // Created by jasonsalex on 13-8-6.
- //
- //
- #include "ButtonSprite.h"
- ButtonSprite * ButtonSprite::create(const char *selFile,const char *disFile )
- {
- ButtonSprite *pRet=new ButtonSprite();
- if(pRet && pRet->init(CCSprite::create(selFile)->displayFrame(),CCSprite::create(disFile)->displayFrame()))
- {
- pRet->autorelease();
- return pRet;
- }
- CC_SAFE_DELETE(pRet);
- return NULL;
- }
- ButtonSprite * ButtonSprite::createWithSpriteFrame(CCSpriteFrame* selFrame,CCSpriteFrame *disFrame)
- {
- ButtonSprite *pRet=new ButtonSprite();
- if(pRet && pRet->init(selFrame,disFrame))
- {
- pRet->autorelease();
- return pRet;
- }
- CC_SAFE_DELETE(pRet);
- return NULL;
- }
- ButtonSprite * ButtonSprite::createWithSpriteFrameName(const char *selFile,const char * disFile)
- {
- ButtonSprite *pRet=new ButtonSprite();
- CCSpriteFrame *selFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(selFile);
- CCSpriteFrame *disFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disFile);
- if(pRet && pRet->init(selFrame,disFrame))
- {
- pRet->autorelease();
- return pRet;
- }
- CC_SAFE_DELETE(pRet);
- return NULL;
- }
- bool ButtonSprite::init(CCSpriteFrame* selFrame,CCSpriteFrame *disFrame)
- {
- this->disSprite=CCSprite::createWithSpriteFrame(disFrame);
- this->selSprite=CCSprite::createWithSpriteFrame(selFrame);
- this->selSprite->retain();
- this->disSprite->retain();
- if(!this->initWithSpriteFrame(selFrame))
- return false;
- return true;
- }
- ButtonSprite::ButtonSprite():m_nScriptTapHandler(0),isEmitTouchEvent(false)
- {
- }
- ButtonSprite::~ButtonSprite()
- {
- CC_SAFE_DELETE(this->disSprite);
- CC_SAFE_DELETE(this->selSprite);
- }
- void ButtonSprite::onEnter()
- {
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
- }
- void ButtonSprite::onExit()
- {
- CCDirector* pDirector = CCDirector::sharedDirector();
- pDirector->getTouchDispatcher()->removeDelegate(this);
- }
- bool ButtonSprite::ccTouchBegan(CCTouch* touch, CCEvent* event)
- {
- this->startTouchPoint = convertToNodeSpace(touch->getLocation());
- this->isEmitTouchEvent= this->getTextureRect().containsPoint(this->startTouchPoint);
- if(isEmitTouchEvent)
- { //如果选择了就显示禁用图像
- this->setDisplayFrame(disSprite->displayFrame());
- return true;
- }else
- {
- return false;
- }
- }
- void ButtonSprite::ccTouchMoved(CCTouch* touch, CCEvent* event)
- {
- float distance=this->startTouchPoint.getDistance(convertToNodeSpace(touch->getLocation()));
- if(abs(distance)<TOUCH_SENSITIVITY) //判断是否超过了移动范围
- {
- this->isEmitTouchEvent=true;
- }else
- {
- this->isEmitTouchEvent=false;
- }
- }
- void ButtonSprite::ccTouchEnded(CCTouch* touch, CCEvent* event)
- {
- if(this->isEmitTouchEvent)
- {
- CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(this->m_nScriptTapHandler,"end");
- }
- this->setDisplayFrame(selSprite->displayFrame()); //恢复图像
- }
- void ButtonSprite::registerScriptTapHandler(int nHandler)
- {
- unregisterScriptTapHandler();
- m_nScriptTapHandler = nHandler;
- }
- void ButtonSprite::unregisterScriptTapHandler(void)
- {
- if (m_nScriptTapHandler)
- {
- CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptTapHandler);
- m_nScriptTapHandler = 0;
- }
- }