cocos2dx --- draw line
1 void DrawLine::draw(){ 2 drawLine(); 3 } 4 void DrawLine::drawLine(){ 5 CCLog("DrawLayer draw line"); 6 int tickSubCount = 20; 7 int pointListKeepCount = 500; 8 for(int i=0; i<tickSubCount; ++i){ 9 if(pointList.size()>0){ 10 pointList.pop_front(); 11 }else{ 12 break; 13 } 14 } 15 while(pointList.size()>pointListKeepCount){ 16 pointList.pop_front(); 17 } 18 float max_lineWidth = 6; 19 float min_lineWidth = 1; 20 int alpha_min = 10; 21 int alpha_max = 200; 22 int R = arc4random()%255; 23 int G = arc4random()%255; 24 int B = arc4random()%255; 25 int pointListCount = pointList.size(); 26 list<CCPoint>::iterator it = pointList.begin(); 27 float pointIndex = 0; 28 for(;it!=pointList.end();++it){ 29 int distanceToMiddle = fabs(pointIndex-pointListCount/2); 30 float percent = 1.0-(float)distanceToMiddle/(float)(pointListCount/2.0); 31 float lineWidth = min_lineWidth + max_lineWidth*percent; 32 int alpha = alpha_min+alpha_max*percent; 33 ccDrawColor4B(R,G,B,alpha); 34 ccPointSize(lineWidth); 35 ccDrawPoint(*it); 36 pointIndex++; 37 } 38 } 39 void DrawLine::ccTouchesBegan(CCSet* touches,CCEvent* event){ 40 CCLog("DrawLayer ccTouchesBegan"); 41 CCSetIterator it = touches->begin(); 42 CCTouch* touch = (CCTouch*)*it; 43 CCPoint beginPoint = touch->getLocationInView(); 44 beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint); 45 pointList.push_back(beginPoint); 46 } 47 void DrawLine::ccTouchesMoved(CCSet* touches,CCEvent* event){ 48 CCLog("DrawLayer ccTouchedMoved"); 49 CCSetIterator it = touches->begin(); 50 CCTouch* touch = (CCTouch*)*it; 51 CCPoint nextPoint = touch->getLocationInView(); 52 nextPoint = CCDirector::sharedDirector()->convertToGL(nextPoint); 53 CCPoint preMovePoint = touch->getPreviousLocationInView(); 54 preMovePoint = CCDirector::sharedDirector()->convertToGL(preMovePoint); 55 float distance = ccpDistance(nextPoint,preMovePoint); 56 if(distance>1){ 57 int d = (int)distance; 58 for(int i=0;i<d;++i){ 59 float distanceX = nextPoint.x-preMovePoint.x; 60 float distanceY = nextPoint.y-preMovePoint.y; 61 float precent = i/distance; 62 CCPoint newPoint; 63 newPoint.x = preMovePoint.x+(distanceX*precent); 64 newPoint.y = preMovePoint.y+(distanceY*precent); 65 pointList.push_back(newPoint); 66 } 67 } 68 } 69 void DrawLine::ccTouchesEnded(CCSet* touches,CCEvent* event){ 70 CCLog("DrawLayer ccTouchedEnded"); 71 pointList.clear(); 72 }
如果ccTouchesXXX事件没有响应,在init方法中设置CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);touch详细细节,可以查看http://www.cnblogs.com/changchao/archive/2013/05/03/3057061.html