[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-02-19 标题cocos2dx学习笔记水果忍者划痕效果分类编程 / C C / cocos2dx 标签cocos2dx·划痕cocos2dx学习笔记水果忍者划痕效果代码备份.h文件.m文件分析最近游戏中用到了划痕效果就在网上google了一下好多资源啊。作者找了一个感觉还不错。在这里备份下。参考【 http://blog.csdn.net/u012945598/article/details/17609281 】。代码备份.h文件#ifndef__RAZER_LAYER_H__#define__RAZER_LAYER_H__#includecocos2d.hclassRazerLayer:publiccocos2d::CCLayer{public:conststaticintTag139269181913;CREATE_FUNC(RazerLayer);virtualboolinit();voiddraw();voiddrawLine();virtualvoidccTouchesBegan(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);virtualvoidccTouchesMoved(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);virtualvoidccTouchesEnded(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);std::listcocos2d::CCPointpointList;};#endif// __RAZER_LAYER_H__.m文件#includeRazerLayer.hUSING_NS_CC;boolRazerLayer::init(){if(!CCLayer::init()){returnfalse;}setTouchEnabled(true);returntrue;}voidRazerLayer::draw(){drawLine();}voidRazerLayer::drawLine(){inttickSubCount10;intpointListKeepCount500;for(inti0;itickSubCount;i){if(pointList.size()0){pointList.pop_front();}else{break;}}while(pointList.size()pointListKeepCount){pointList.pop_front();}floatmax_lineWidth5;floatmin_lineWidth1;intalpha_min10;intalpha_max200;intR100;//arc4random()%255;intG100;//arc4random()%255;intB100;//arc4random()%255;intpointListCountpointList.size();std::listCCPoint::iterator itpointList.begin();floatpointIndex0;for(;it!pointList.end();it){intdistanceToMiddlefabs(pointIndex-pointListCount/2);floatpercent1.0-(float)distanceToMiddle/(float)(pointListCount/2.0);floatlintWidthmin_lineWidthmax_lineWidth*percent;intalphaalpha_minalpha_max*percent;ccc4(R,G,B,alpha);ccPointSize(lintWidth);ccDrawPoint(*it);pointIndex;}}voidRazerLayer::ccTouchesBegan(CCSet*pTouches,CCEvent*pEvent){CCSetIterator itpTouches-begin();CCTouch*touch(CCTouch*)*it;CCPoint beginPointtouch-getLocationInView();beginPointCCDirector::sharedDirector()-convertToGL(beginPoint);// beginPoint 检测pointList.push_back(beginPoint);}voidRazerLayer::ccTouchesMoved(CCSet*pTouches,CCEvent*pEvent){CCSetIterator itpTouches-begin();CCTouch*touch(CCTouch*)*it;CCPoint nextPointtouch-getLocationInView();nextPointCCDirector::sharedDirector()-convertToGL(nextPoint);// nextPoint 检测CCPoint preMovePointtouch-getPreviousLocationInView();preMovePointCCDirector::sharedDirector()-convertToGL(preMovePoint);floatdistanceccpDistance(nextPoint,preMovePoint);if(distance1){intd(int)distance;for(inti0;id;i){floatdistanceXnextPoint.x-preMovePoint.x;floatdistanceYnextPoint.y-preMovePoint.y;floatpercenti/distance;CCPoint newPoint;newPoint.xpreMovePoint.x(distanceX*percent);newPoint.ypreMovePoint.y(distanceY*percent);pointList.push_back(newPoint);}}}voidRazerLayer::ccTouchesEnded(CCSet*pTouches,CCEvent*pEvent){pointList.clear();}分析代码原理比较清晰在touchmove的回调方法中获取两个手势点之间的所有的像素点让后将存储起来然后在每帧的绘制中根据每个点的位置设定不同的线宽然后将这个点绘制出来。就形成了划痕效果。如果要在指定的范围内进行划痕处理只要处理代码中的指定点即可。