I have some problems when I try to draw lines with cocos2d! I store the points obtained from the touchMoved method in an NSMutableArray and pass this array to a CCNode subclass called Lines, which I use to draw strings from an array of points. The problem is that the line is not smooth when I scroll slowly, but when I scroll faster, the line is much smoother. See the pictures below:
Slow Swipe: 
Quick whistle: 
I tried to solve the problem with ccpDistance, which calculates the distance between the last saved point, and if it is not far enough, I do not save it. I also tried to draw small circles in each saved position, but this is also not very nice. Here is my code:
In my GameScene:
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; if (ccpDistance(lastPoint, location) > 10) {
And my Line class:
- (void) updatePoints:(NSMutableArray *)_point { points = _point; } - (void) draw { if ([points count] > 0) { ccGLEnable(GL_LINE_STRIP); ccDrawColor4B(209, 75, 75, 255); float lineWidth = 6.0 * CC_CONTENT_SCALE_FACTOR(); glLineWidth(lineWidth); int count = [points count]; for (int i = 0; i < (count - 1); i++){ CGPoint pos1 = [[points objectAtIndex:i] CGPointValue]; CGPoint pos2 = [[points objectAtIndex:i+1] CGPointValue]; ccDrawLine(pos1, pos2); ccDrawSolidCircle(pos2, 2.5, 20); } } }
Also, is there something in my code that can be done better to improve performance? Right now I have no problem even with 1000+ points, but just in case ...
Any help would be greatly appreciated! Thanks in advance!
source share