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

dev*_*546 8 touchesmoved ios7 sprite-kit

我想沿着touchesmoved中收集的点在sprite kit中画一条线.

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

有任何想法吗?

mem*_*eep 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
Run Code Online (Sandbox Code Playgroud)