在touchesmoved中用sprite工具包画一条线

时间:2013-11-13 12:29:58

标签: ios7 sprite-kit touchesmoved

我想沿着touchesmoved收集的点在sprite工具包中画一条线。

最有效的方法是什么?我已经尝试了几次,我的线在y轴上是错误的,或者占用了很多处理能力,fps下降到10秒。

有什么想法吗?

1 个答案:

答案 0 :(得分:17)

您可以定义CG路径并通过在触摸移动功能中添加线条或弧线来修改它。之后,您可以从路径创建SKShapeNode并根据需要进行配置。 如果要在手指在屏幕上移动时绘制线条,则可以在触摸以空路径开始时创建形状节点,然后对其进行修改。

编辑:我写了一些代码,它对我有用,画了一条简单的红线。

在你的MyScene.m中:

@interface MyScene()
{
    CGMutablePathRef pathToDraw;
    SKShapeNode *lineNode;
}
@end

@implementation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    lineNode.strokeColor = [SKColor redColor];
    [self addChild:lineNode];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
    lineNode.path = pathToDraw;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// delete the following line if you want the line to remain on screen.
    [lineNode removeFromParent];
    CGPathRelease(pathToDraw);
}
@end