</pre><pre name="code" >用于环形滑动,选择英雄
<img src="http://img.blog.csdn.net/20141008203419127?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHl6MTQ1MTc4NDE0NQ==/Font/5a6L5L2T/Fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
</pre><pre name="code" >//MyRoleScrollVIEw.h#pragma once #include "cocos2d.h"USING_NS_CC;class nodeAndPt:public CCObject{public: nodeAndPt(CCNode *node,int ptindex) { this->node = node; this->ptIndex = ptindex; } ~nodeAndPt() { } int ptIndex; CCNode *node;};class MyRoleScrollVIEw:public CCNode,public CCtouchDelegate{public: MyRoleScrollVIEw():maxnum(6),istouchDown(false),isMoveTimeOut(true) {} ~MyRoleScrollVIEw() { } static MyRoleScrollVIEw * create(CCArray * nodeArr = NulL,int cgm = 60,int radius = 200); voID addRole(CCNode * node);private: bool init(CCArray * nodeArr,int cgm,int radius); voID initData(CCArray * nodeArr,int radius); voID initVIEw(); virtual voID onEnter(); virtual voID onExit(); 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); voID setPt(nodeAndPt * p,int i); voID refreshPt(); voID updateOnce(float t);private: CC_SYNTHESIZE(CCNode *,_currentNode,CurrentNode); const int maxnum; int roleNum; CCArray * _nodeArr; bool istouchDown; CCPoint beginPt; voID blackSprite(CCNode* sprite); voID recoverFromBalck(CCNode* sprite); std::vector<CCPoint> ptVec; int cgm;//椭圆的倾斜角度 int radius;//半径 bool isMoveTimeOut;//一次滑动结束,才能开启下一次滑动};
</pre><pre name="code" ><pre name="code" >两个类,<span >nodeAndPt 绑定每个节点和坐标。</span>
<span ></span><pre name="code" >MyRoleScrollVIEw负责滑动
create函数有三个参数,第一个是数组,每一个都是节点,可以是精灵或者panel,可以包含子节点。
第二参数和第三参数分别是圆的半径和圆的倾斜度数
</pre><pre name="code" >接下来,大家自己看cpp文件吧。
</pre><pre name="code" >
</pre><pre name="code" >
//MyRoleScrollVIEw.cpp#include "MyRoleScrollVIEw.h"#define SHADE_TAG 1234#define induration 0.2f#define _scale 0.6f#define PIE 3.141592653MyRoleScrollVIEw * MyRoleScrollVIEw::create(CCArray * nodeArr,int radius){ MyRoleScrollVIEw * node = new MyRoleScrollVIEw; if(node&&node->init(nodeArr,cgm,radius)) { node->autorelease(); } else delete node; return node;}voID MyRoleScrollVIEw::addRole(CCNode * node){ if(!node) return; if(_nodeArr->count()<this->maxnum) { nodeAndPt * p = new nodeAndPt(node,roleNum); _nodeArr->addobject(p); roleNum++; } refreshPt();}bool MyRoleScrollVIEw::init(CCArray * nodeArr,int radius){ if(!CCNode::init()) return false; initData(nodeArr,radius); initVIEw(); return true;}voID MyRoleScrollVIEw::initData(CCArray * nodeArr,int radius){ if(!nodeArr||nodeArr->count()<1) return; this->cgm = cgm; this->radius = radius; _nodeArr = CCArray::create(); _nodeArr->retain(); for(int i = 0;i<nodeArr->count();i++) { CCNode * node = dynamic_cast<CCNode*>(nodeArr->objectAtIndex(i)); nodeAndPt * p = new nodeAndPt(node,i); _nodeArr->addobject(p); if(i==0) { _currentNode = node; } } roleNum = _nodeArr->count(); refreshPt();}voID MyRoleScrollVIEw::initVIEw(){ CCPoint pt = this->getposition(); for(int i = 0;i<roleNum;i++) { nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i)); CCNode *node = p->node; if(i==0) { _currentNode = p->node; // this->recoverFromBalck(dynamic_cast<CCSprite*>(node)); p->node->setScale(1.0f); } else { // this->blackSprite(dynamic_cast<CCSprite*>(node)); p->node->setScale(_scale); } node->setposition(ptVec[p->ptIndex]); this->addChild(node,i); } schedule(schedule_selector(MyRoleScrollVIEw::updateOnce),0.8f);}voID MyRoleScrollVIEw::onEnter(){ CCNode::onEnter(); CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,-1,true);}voID MyRoleScrollVIEw::onExit(){ CCNode::onExit(); CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);}bool MyRoleScrollVIEw::cctouchBegan(CCtouch *ptouch,CCEvent *pEvent){ if(roleNum<2) return false; CCNode * node= _currentNode; CCPoint pt = node->convertToNodeSpace(ptouch->getLocation()); CCRect rect(-node->getContentSize().wIDth,node->getContentSize().wIDth*3,node->getContentSize().height); if(rect.containsPoint(pt)) { istouchDown = true; beginPt = ptouch->getLocation(); } return true;}voID MyRoleScrollVIEw::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){ if(!istouchDown||!isMoveTimeOut) return; CCPoint pt = ptouch->getLocation(); int moveX = pt.x-beginPt.x; if(moveX>50)//向右滑 { istouchDown = false; for(int i = 0;i<roleNum;i++) { nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i)); setPt(p,p->ptIndex+1); } isMoveTimeOut = false; scheduleOnce(schedule_selector(MyRoleScrollVIEw::updateOnce),induration); } else if(moveX<-50)//向左划 { istouchDown = false; for(int i = 0;i<roleNum;i++) { nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i)); setPt(p,p->ptIndex-1); } isMoveTimeOut = false; scheduleOnce(schedule_selector(MyRoleScrollVIEw::updateOnce),induration); }}voID MyRoleScrollVIEw::cctouchended(CCtouch *ptouch,CCEvent *pEvent){ istouchDown = false;}voID MyRoleScrollVIEw::cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent){}voID MyRoleScrollVIEw::refreshPt(){ ptVec.clear(); int angle = 360/roleNum; for(int i = 0,j = -90.0f;i<roleNum;i++) { double hudu = j*PIE/180; float g_sin = sin(hudu); float g_cos = cos(hudu); int x = MyRoleScrollVIEw::radius*g_cos; int y = MyRoleScrollVIEw::radius*g_sin*cos(cgm*PIE/180); ptVec.push_back(CCPoint(x,y)); j+=angle; }}voID MyRoleScrollVIEw::setPt(nodeAndPt * p,int i){ if(i == -1) i = roleNum-1; else if(i == roleNum) i = 0; ccBezIErConfig tr0; float a = p->node->getposition().x; float b = p->node->getposition().y; float c = ptVec[i].x; float d = ptVec[i].y; float m = (a+c)/4; float n = (b+d)/4; CCPoint pt1 = ccpAdd(p->node->getposition(),ccp(m,n)); CCPoint pt2 = ccpAdd(ptVec[i],n)); tr0.endposition=ptVec[i]; tr0.controlPoint_1=pt1; tr0.controlPoint_2=pt2; if(i==0) { _currentNode = p->node; this->recoverFromBalck(dynamic_cast<CCSprite*>(p->node)); } else { this->blackSprite(dynamic_cast<CCSprite*>(p->node)); } CCActionInterval* bezIErForward = CCBezIErTo::create(induration,tr0); CCSpawn * sp=NulL; if(i==0) sp = CCSpawn::create(CCScaleto::create(induration,1),bezIErForward,NulL); else sp = CCSpawn::create(CCScaleto::create(induration,_scale),NulL); p->node->runAction(sp); p->ptIndex = i;}voID MyRoleScrollVIEw::blackSprite(CCNode* sprite){ //unsigned int wIDth = sprite->getTexture()->getPixelsWIDe(); //unsigned int height = sprite->getTexture()->getPixelsHigh(); if(sprite->getChildByTag(SHADE_TAG)!=NulL) return; CCPoint position = sprite->getposition(); unsigned int wIDth = sprite->getContentSize().wIDth; unsigned int height = sprite->getContentSize().height; CCRenderTexture* r = CCRenderTexture::create(wIDth,height); r->beginWithClear(1,1,0); sprite->setposition(ccp(wIDth / 2.0,height/ 2.0)); // Node: set position here! sprite->visit(); r->end(); // create a new CCImage CCImage* image = r->newCCImage(); wIDth = image->getWIDth(); height= image->getHeight(); // this data is the texture data in memery unsigned char* data = image->getData(); typedef enum { RED = 0,GREEN = 1,BLUE = 2,Alpha = 3 } PIXELS; // convert unsigned char*(1 Byte) to uint_32_t(4 Bytes) uint32_t *pixels = (uint32_t *)data; for(int y = 0; y < height; y++) { for(int x = 0; x < wIDth; x++) { uint8_t *rgbAPIxel = (uint8_t *) &pixels[y * wIDth + x]; uint32_t gray = 0.3 * rgbAPIxel[RED] + 0.59 * rgbAPIxel[GREEN] + 0.11 * rgbAPIxel[BLUE]; //uint32_t gray = 0.3 * rgbAPIxel[RED] + 0.3 * rgbAPIxel[GREEN] + 0.11 * rgbAPIxel[BLUE]; // set the pixels to gray rgbAPIxel[RED] = gray; rgbAPIxel[GREEN] = gray; rgbAPIxel[BLUE] = gray; } } // create a new CCTexture2D based on the CCImage data modifIEd above CCTexture2D* texture = new CCTexture2D(); texture->initWithImage(image); CCSprite * s = CCSprite::create(); s->initWithTexture(texture); // release other resources r->release(); image->release(); CCArray * arrChild = sprite->getChildren(); CCObject *obj; CCARRAY_FOREACH(arrChild,obj) { CCNode * node = dynamic_cast<CCNode*>(obj); node->setVisible(false); } s->setAnchorPoint(ccp(0,0)); s->setposition(ccp(0,0)); s->setopacity(200); s->setTag(SHADE_TAG); sprite->addChild(s); sprite->setposition(position);}voID MyRoleScrollVIEw::recoverFromBalck(CCNode* sprite){ if(sprite->getChildByTag(SHADE_TAG)==NulL) return; sprite->removeChildByTag(SHADE_TAG); CCArray * arrChild = sprite->getChildren(); CCObject *obj; CCARRAY_FOREACH(arrChild,obj) { CCNode * node = dynamic_cast<CCNode*>(obj); node->setVisible(true); }}voID MyRoleScrollVIEw::updateOnce(float t){ isMoveTimeOut = true; for(int i = 0;i<roleNum;i++) { nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i)); setPt(p,p->ptIndex+1); } isMoveTimeOut = false;}总结
以上是内存溢出为你收集整理的cocos2d-x 环形滑动控件全部内容,希望文章能够帮你解决cocos2d-x 环形滑动控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)