我嘗試使用cocos2d繪製線條時遇到一些麻煩!我將touchMoved方法得到的點存儲在一個NSMutableArray中,並將該數組傳遞給CCNode的一個子類,稱爲Lines,用於從點數組中繪製線條。問題是,當我慢慢地滑動時,線條不平滑,但是當我滑動得更快時,線條更平滑。見下面的圖片:Cocos2D&ccDrawLine - 繪製流暢的線條
慢刷卡:
快速掃:
我試圖解決ccpDistance的問題,計算最後保存點之間的距離,如果它還不夠遠我不保存它。我也試圖在每個已保存的位置畫出小圓圈,但這也不是很好。這裏是我的代碼:
在我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+點,但以防萬一...
任何幫助將不勝感激!提前致謝!
對於100%平滑和無干擾的線條,您必須將整條線條繪製爲單個多邊形,否則您總會在兩條線條相遇處留下微小的切口 – LearnCocos2D