Drawing straight line with spritekit and UITouch c

2019-09-02 17:59发布

Should be simple.

I'm trying to draw a single, straight line using UITouch and Spritekit. However, when touchesmoved is called, it creates multiple lines instead of just a single line. Code used is below:

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

    pathToDraw = CGPathCreateMutable();

    selectorLine = [SKShapeNode node];
    selectorLine.strokeColor = [SKColor greenColor];
    selectorLine.lineWidth = 5;
    [self addChild:selectorLine];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    pathToDraw2 = CGPathCreateMutable();
    UITouch* touch = [touches anyObject];
    positionInScene2 = [touch locationInNode:self];

    CGPathMoveToPoint(pathToDraw2, NULL, positionInScene1.x, positionInScene1.y);
    CGPathAddLineToPoint(pathToDraw2, NULL, positionInScene2.x, positionInScene2.y);
    CGPathCloseSubpath(pathToDraw);

    selectorLine.path = pathToDraw;
}

If I move

CGPathAddLineToPoint(pathToDraw, NULL, positionInScene2.x, positionInScene2.y);

to touchesEnd, it creates a single line but only after the user ends the touch. I want the user to see the line being drawn as they touch.

Thanks, Doug

1条回答
地球回转人心会变
2楼-- · 2019-09-02 18:10

Create a new path in your touchesMoved. You are modifying the same path, and just adding more and more lines to it.

查看更多
登录 后发表回答