在CCScrollView中添加ccmenu实现滑动效果是不可能的,因为ccmenu的触发事件是你在touchBegan就全部捕获掉了,如果你想滑动CCScrollView取消选中这个菜单是无法实现的,.所以我们应该自己编写一个模拟ccmenu菜单的控件,在按下按钮后,如果不移动按钮的话,就触发触摸事件,移动的话就取消触摸事件,实现按钮在CCScrollView中的滚动效果。。。
开发人员:Jason's.Alex QQ:531401335
csdn博客:http://blog.csdn.net/RuShrooM
- //
- //CCButtonSptite.h
- //CCSpriteButton
- //
- //Createdbyjasonsalexon13-8-6.
- //
- //
- #ifndef__CCSpriteButton__CCButtonSptite__
- #define__CCSpriteButton__CCButtonSptite__
- #include"cocos2d.h"
- #defineTOUCH_SENSITIVITY20//触摸灵敏度
- usingnamespacecocos2d;
- classButtonSprite:publicCCSprite,publicCCTouchDelegate
- {
- public:
- ButtonSprite();
- virtualboolinit(CCSpriteFrame*selFile,CCSpriteFrame*disFile);
- staticButtonSprite*create(constchar*selFile,constchar*disFile);
- staticButtonSprite*createWithSpriteFrame(CCSpriteFrame*selFrame,CCSpriteFrame*disFrame);
- staticButtonSprite*createWithSpriteFrameName(constchar*selFile,constchar*disFile);
- virtual~ButtonSprite();
- virtualvoidonEnter();
- virtualvoidonExit();
- virtualboolccTouchBegan(CCTouch*touch,CCEvent*event);
- virtualvoidccTouchMoved(CCTouch*touch,CCEvent*event);
- virtualvoidccTouchEnded(CCTouch*touch,CCEvent*event);
- voidregisterScriptTapHandler(intnHandler);
- voidunregisterScriptTapHandler(void);
- private:
- CCSprite*selSprite;//选择的精灵
- CCSprite*disSprite;//不选择的精灵
- intm_nScriptTapHandler;//脚本函数句柄
- boolisEmitTouchEvent;//是否发射触摸事件
- CCPointstartTouchPoint;//开始的触摸坐标
- };
- #endif/*defined(__CCSpriteButton__CCButtonSptite__)*/
- //
- //CCButtonSptite.cpp
- //CCSpriteButton
- //
- //Createdbyjasonsalexon13-8-6.
- //
- //
- #include"ButtonSprite.h"
- ButtonSprite*ButtonSprite::create(constchar*selFile,constchar*disFile)
- {
- ButtonSprite*pRet=newButtonSprite();
- if(pRet&&pRet->init(CCSprite::create(selFile)->displayFrame(),CCSprite::create(disFile)->displayFrame()))
- {
- pRet->autorelease();
- returnpRet;
- }
- CC_SAFE_DELETE(pRet);
- returnNULL;
- }
- ButtonSprite*ButtonSprite::createWithSpriteFrame(CCSpriteFrame*selFrame,CCSpriteFrame*disFrame)
- {
- ButtonSprite*pRet=newButtonSprite();
- if(pRet&&pRet->init(selFrame,disFrame))
- {
- pRet->autorelease();
- returnpRet;
- }
- CC_SAFE_DELETE(pRet);
- returnNULL;
- }
- ButtonSprite*ButtonSprite::createWithSpriteFrameName(constchar*selFile,constchar*disFile)
- {
- ButtonSprite*pRet=newButtonSprite();
- CCSpriteFrame*selFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(selFile);
- CCSpriteFrame*disFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(disFile);
- if(pRet&&pRet->init(selFrame,disFrame))
- {
- pRet->autorelease();
- returnpRet;
- }
- CC_SAFE_DELETE(pRet);
- returnNULL;
- }
- boolButtonSprite::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))
- returnfalse;
- returntrue;
- }
- ButtonSprite::ButtonSprite():m_nScriptTapHandler(0),isEmitTouchEvent(false)
- {
- }
- ButtonSprite::~ButtonSprite()
- {
- CC_SAFE_DELETE(this->disSprite);
- CC_SAFE_DELETE(this->selSprite);
- }
- voidButtonSprite::onEnter()
- {
- CCDirector*pDirector=CCDirector::sharedDirector();
- pDirector->getTouchDispatcher()->addTargetedDelegate(this,false);
- }
- voidButtonSprite::onExit()
- {
- CCDirector*pDirector=CCDirector::sharedDirector();
- pDirector->getTouchDispatcher()->removeDelegate(this);
- }
- boolButtonSprite::ccTouchBegan(CCTouch*touch,CCEvent*event)
- {
- this->startTouchPoint=convertToNodeSpace(touch->getLocation());
- this->isEmitTouchEvent=this->getTextureRect().containsPoint(this->startTouchPoint);
- if(isEmitTouchEvent)
- {//如果选择了就显示禁用图像
- this->setDisplayFrame(disSprite->displayFrame());
- returntrue;
- }else
- {
- returnfalse;
- }
- }
- voidButtonSprite::ccTouchMoved(CCTouch*touch,CCEvent*event)
- {
- floatdistance=this->startTouchPoint.getDistance(convertToNodeSpace(touch->getLocation()));
- if(abs(distance)<TOUCH_SENSITIVITY)//判断是否超过了移动范围
- {
- this->isEmitTouchEvent=true;
- }else
- {
- this->isEmitTouchEvent=false;
- }
- }
- voidButtonSprite::ccTouchEnded(CCTouch*touch,CCEvent*event)
- {
- if(this->isEmitTouchEvent)
- {
- CCScriptEngineManager::sharedManager()->getScriptEngine()->executeEvent(this->m_nScriptTapHandler,"end");
- }
- this->setDisplayFrame(selSprite->displayFrame());//恢复图像
- }
- voidButtonSprite::registerScriptTapHandler(intnHandler)
- {
- unregisterScriptTapHandler();
- m_nScriptTapHandler=nHandler;
- }
- voidButtonSprite::unregisterScriptTapHandler(void)
- {
- if(m_nScriptTapHandler)
- {
- CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptTapHandler);
- m_nScriptTapHandler=0;
- }
- }