2016-08-19 5 views
0

私のSpriteKitシーンでは、指で線を描くことができるはずです。SpriteKitで線を効率的に描画する方法

enter image description here

mylineSKShapeNode*)、私が収集描くために:私は、ラインが長い場合、FPSは4-6まで取得し、行は以下の画像のように、多角形の取得を開始し、作業溶液を持っていますこのように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]; 
    } 
} 
を線を引きます3210

この問題を解決するにはどうすればよいですか?それ以降のすべての線はすべて多角形であるため、fpsが非常に低く、タッチのサンプリングレートも自動的に非常に低くなるので、私は思っています...

私はiPad 3を使用しました

答えて

3

いつも新しいSKShapeNodesを作成しないでください、あなたの減速の原因となっているバグがあります。その代わり、唯一の1 SKShapeNode(または束を作成するが、それらを再利用)を使用して、新しい情報でパスを追加し(だから、常にバックグラウンドにマイラインを追加する必要はありません)

オルタナティブ:

使用1コミュニティSKSkapeNodeは、パスのレンダリングのために、そして、その後、代わりに私のユーザーは、彼/彼女が望む限り多くの線を描画することができるはず

+0

シェイプノードのこの新しいテクスチャをSKSpriteNodeを追加し、view.textureFromNodeでテクスチャにSKShapeNodeを変換しますとにかく、私はいつも同じSKShapeNodeを再利用しようとします。あなたの素早い答えをありがとう! – ddb

+0

NP、おそらく彼らはiOS 10のSKShapeNodesを修正するでしょう – Knight0fDragon

+1

@ddb私はあなたのためにうまくいくかもしれない代替の答えを投稿しました – Knight0fDragon

関連する問題