cocos2d-x 环形滑动控件

</pre><pre name="code" class="cpp">用于环形滑动,选择英雄
<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" class="cpp">//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" class="cpp"><pre name="code" class="cpp">两个类,<span style="font-family: Arial,Helvetica,sans-serif;">nodeAndPt 绑定每个节点和坐标。</span>
<span style="font-family: Arial,sans-serif;"></span><pre name="code" class="cpp">MyRoleScrollView负责滑动
create函数有三个参数,第一个是数组,每一个都是节点,可以是精灵或者panel,可以包含子节点。
第二参数和第三参数分别是圆的半径和圆的倾斜度数
</pre><pre name="code" class="cpp">接下来,大家自己看cpp文件吧。
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">
//MyRoleScrollView.cpp

#include "MyRoleScrollView.h"

#define SHADE_TAG 1234
#define induration 0.2f
#define _scale 0.6f

#define PIE 3.141592653




MyRoleScrollView * 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;
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...