cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件

cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件,第1张

概述在CCScrollView中添加ccmenu实现滑动效果是不可能的,因为ccmenu的触发事件是你在touchBegan就全部捕获掉了,如果你想滑动CCScrollView取消选中这个菜单是无法实现的,.所以我们应该自己编写一个模拟ccmenu菜单的控件 ,在按下按钮后,如果不移动按钮的话,就触发触摸事件,移动的话就取消触摸事件,实现按钮在CCScrollView中的滚动效果。。。 开发人员:Ja

在CCScrollVIEw中添加ccmenu实现滑动效果是不可能的,因为ccmenu的触发事件是你在touchBegan就全部捕获掉了,如果你想滑动CCScrollVIEw取消选中这个菜单是无法实现的,.所以我们应该自己编写一个模拟ccmenu菜单的控件,在按下按钮后,如果不移动按钮的话,就触发触摸事件,移动的话就取消触摸事件,实现按钮在CCScrollVIEw中的滚动效果。。。

开发人员:Jason's.Alex QQ:531401335

csdn博客:http://blog.csdn.net/rushrooM


[cpp] view plain copy // //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__)*/
[cpp] view plain copy // //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) { @R_419_4189@EngineManager::sharedManager()->getScriptEngine()->executeEvent(this->m_nScriptTapHandler,"end"); } this->setdisplayFrame(selSprite->displayFrame());//恢复图像 } voIDbuttonSprite::registerScriptTapHandler(intnHandler) { unregisterScriptTapHandler(); m_nScriptTapHandler=nHandler; } voIDbuttonSprite::unregisterScriptTapHandler(voID) { if(m_nScriptTapHandler) { @R_419_4189@EngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptTapHandler); m_nScriptTapHandler=0; } } 总结

以上是内存溢出为你收集整理的cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件全部内容,希望文章能够帮你解决cocos2d实现类似CCMenu菜单类控件,在CCScrollView滚动, lua脚本控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1023698.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存