0
在我的SpriteKit場景中,用戶應該能夠用他/她的手指畫線。我有一個工作的解決方案,如果線路長,FPS下來到4-6和線路開始變得多邊形,如下圖:如何在SpriteKit中高效地繪製線條
要繪製myline
(SKShapeNode*
),我收集以這種方式在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)
我的用戶應該能夠爲他/她希望吸引儘可能多的線路添加
SKSpriteNode
,無論如何,我會盡量重複使用相同的SKShapeNode。感謝您的快速回答! – ddbNP,也許他們會解決SKShapeNodes在iOS的10 – Knight0fDragon
@ddb我發佈了一個備選答案可能制定出更好的爲您 – Knight0fDragon