在我的SpriteKit场景中,用户应该能够用他的手指画线。我有一个可行的解决方案,如果线很长,FPS降至4-6,并且线开始变为多边形,如下图所示:
为了绘制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(我的应用需要在具有iOS7的iPad 2型号上运行)
最佳答案
不要不断创建新的SKShapeNodes
,这是导致您的速度下降的错误。相反,仅使用1 SKShapeNode
(或创建一堆然后重用它们),并在路径后附加新信息(因此无需将myline不断添加到后台)
选择:
使用1个社区SKSkapeNode
渲染路径,然后将SKShapeNode
转换为具有view.textureFromNode
的纹理,然后添加具有此新纹理而不是shape节点的SKSpriteNode
关于ios - 如何有效地在SpriteKit中画一条线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39039763/