Cocos2d-x 水果忍者划痕效果

网上找的一个关于水果忍者划痕的,效果还算凑合。其原理就是基于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();

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值