当我尝试用 cocos2d 画线时,我遇到了一些麻烦!我将从 touchMoved 方法获得的点存储在 NSMutableArray 中,并将该数组传递给 CCNode 的子类,称为 Lines,我使用它从点数组中绘制线。问题是当我缓慢滑动时线条不平滑,但是当我快速滑动时,线条要平滑得多。请看下面的图片:
慢速滑动:
快速滑动:
我试图用 ccpDistance 解决这个问题,它计算最后保存的点之间的距离,如果它不够远,我就不保存它。我还尝试在每个保存的位置上画小圆圈,但这也不是很好。这是我的代码:
在我的游戏场景中:
- (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+点我也没有任何问题,但以防万一......
任何帮助将不胜感激!提前致谢!