2016-08-19 98 views
0

在我的SpriteKit場景中,用戶應該能夠用他/她的手指畫線。我有一個工作的解決方案,如果線路長,FPS下來到4-6和線路開始變得多邊形,如下圖:如何在SpriteKit中高效地繪製線條

enter image description here

要繪製mylineSKShapeNode*),我收集以這種方式在NSMutableArray* noteLinePoints觸摸運動的點

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint touchPoint = [[touches anyObject] locationInNode:self.scene]; 
    SKNode *node = [self nodeAtPoint:touchPoint]; 

    if(noteWritingActive) 
    { 
     [noteLinePoints removeAllObjects]; 
     touchPoint = [[touches anyObject] locationInNode:_background]; 
     [noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]]; 
     myline = (SKShapeNode*)node; 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if(myline) 
    { 
     CGPoint touchPoint = [[touches anyObject] locationInNode:_background]; 
     [noteLinePoints addObject:[NSValue valueWithCGPoint:touchPoint]]; 
     [self drawCurrentNoteLine]; 
    } 
} 

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 
{ 
    if(myline) 
    { 
     myline.name = @"note"; 
     myline = nil; 
    } 
    NSLog(@"touch ended"); 
} 

和我劃清界線以這種方式

- (CGPathRef)createPathOfCurrentNoteLine 
{ 
    CGMutablePathRef ref = CGPathCreateMutable(); 

    for(int i = 0; i < [noteLinePoints count]; ++i) 
    { 
     CGPoint p = [noteLinePoints[i] CGPointValue]; 
     if(i == 0) 
     { 
      CGPathMoveToPoint(ref, NULL, p.x, p.y); 
     } 
     else 
     { 
      CGPathAddLineToPoint(ref, NULL, p.x, p.y); 
     } 
    } 

    return ref; 
} 

- (void)drawCurrentNoteLine 
{ 
    if(myline) 
    { 
     SKNode* oldLine = [self childNodeWithName:@"line"]; 
     if(oldLine) 
      [self removeChildrenInArray:[NSArray arrayWithObject:oldLine]]; 

     myline = nil; 

     myline = [SKShapeNode node]; 
     myline.name = @"line"; 
     [myline setStrokeColor:[SKColor grayColor]]; 

     CGPathRef path = [self createPathOfCurrentNoteLine]; 
     myline.path = path; 
     CGPathRelease(path); 

     [_background addChild:myline]; 
    } 
} 

我該如何解決這個問題?因爲所有後續的行都只是多邊形,我認爲是因爲fps非常低,觸摸的採樣率自動也很低...

請注意,對於測試,我使用了iPad 3(我的應用程序需要從iPad 2的模型正在與iOS7)

回答

3

不要不斷創造新的SKShapeNodes,還有這是造成您經濟放緩的錯誤。相反,只使用1 SKShapeNode(或創建了一堆,但重複使用它們),並追加新的信息的路徑(所以沒有需要不斷的MYLINE增加背景)

備選:

使用1社區SKSkapeNode渲染的路徑,然後轉換SKShapeNode到紋理與view.textureFromNode,然後用這個新的紋理,而不是形狀節點

+0

我的用戶應該能夠爲他/她希望吸引儘可能多的線路添加SKSpriteNode,無論如何,我會盡量重複使用相同的SKShapeNode。感謝您的快速回答! – ddb

+0

NP,也許他們會解決SKShapeNodes在iOS的10 – Knight0fDragon

+1

@ddb我發佈了一個備選答案可能制定出更好的爲您 – Knight0fDragon