网上找的一个关于水果忍者划痕的,效果还算凑合。其原理就是基于OpenGL绘制直线,因为版本号过老,此处笔者改动了一些方法,粘贴后可直接使用
适用于Cocos2d-x 2.2.1
.h文件里须要添?的代码:
void draw();
void drawLine();
virtual void ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);
std::list<CCPoint> pointList;
.cpp文件里
void HelloWorld::draw()
{
drawLine();
}
void HelloWorld::drawLine()
{
int tickSubCount = 10;
int pointListKeepCount = 500;
for (int i=0; i<tickSubCount ; i++)
{
if (pointList.size() >0)
{
pointList.pop_front();
}
else
{
break;
}
}
while (pointList.size() > pointListKeepCount)
{
pointList.pop_front();
}
float max_lineWidth = 5;
float min_lineWidth = 1;
int alpha_min = 10;
int alpha_max = 200;
int R = arc4random()%255;
int G = arc4random()%255;
int B = arc4random()%255;
int pointListCount = pointList.size();
std::list <CCPoint>::iterator it =pointList.begin();
float pointIndex = 0;
for(;it!=pointList.end();it++)
{
int distanceToMiddle = fabs(pointIndex-pointListCount/2);
float percent = 1.0-(float)distanceToMiddle/(float)(pointListCount/2.0);
float lintWidth = min_lineWidth + max_lineWidth*percent;
int alpha = alpha_min +alpha_max*percent;
ccc4(R,G,B,alpha );
ccPointSize(lintWidth);
ccDrawPoint( *it );
pointIndex++;
}
}
void HelloWorld::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)*it;
CCPoint beginPoint = touch->getLocationInView();
beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint);
pointList.push_back(beginPoint);
}
void HelloWorld::ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)*it;
CCPoint nextPoint = touch->getLocationInView( );
nextPoint = CCDirector::sharedDirector()->convertToGL(nextPoint);
CCPoint preMovePoint = touch->getPreviousLocationInView();
preMovePoint = CCDirector::sharedDirector()->convertToGL(preMovePoint);
float distance = ccpDistance(nextPoint, preMovePoint);
if (distance > 1)
{
int d = (int)distance;
for (int i =0; i < d; i++ )
{
float distanceX = nextPoint.x - preMovePoint.x;
float distanceY = nextPoint.y - preMovePoint.y;
float percent = i / distance;
CCPoint newPoint;
newPoint.x = preMovePoint.x + (distanceX * percent);
newPoint.y = preMovePoint.y + (distanceY * percent);
pointList.push_back(newPoint);
}
}
}
void HelloWorld::ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent)
{
pointList.clear();
}
Cocos2d-x 水果忍者划痕效果,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/yxwkf/p/3819484.html