我在使用cocos2d绘制线条时遇到了一些问题!我将从touchMoved方法得到的点存储在NSMutableArray中,并将该数组传递给CCNode子类Lines,以便从点数组绘制线条。问题是,当我缓慢滑动时,线条不流畅,但当我快速滑动时,线条则更加平滑。请参见下面的图片:
慢滑动:
快速滑动:
我尝试使用ccpDistance来解决这个问题,它可以计算上一个保存点与当前点之间的距离,如果距离不够远,就不会保存它。我还尝试在每个保存的位置上绘制小圆圈,但这也不是很好看。以下是我的代码:
在我的GameScene中:
慢滑动:
![Slow swipe](https://istack.dev59.com/9FTbt.webp)
![Fast swipe](https://istack.dev59.com/2bc6S.webp)
在我的GameScene中:
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if (ccpDistance(lastPoint, location) > 10) {
//SAVE THE POINT
[linePoints addObject:[NSValue valueWithCGPoint:location]];
[line updatePoints:linePoints];
lastPoint = location;
}
}
我的线路类:
- (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);
}
}
}
另外,我的代码中是否有可以改进以提高性能的地方?目前即使有1000多个点也没有任何问题,但以防万一...
非常感谢您的帮助!提前致谢!