Drawing straight line with spritekit and UITouch creates multiple lines

拈花ヽ惹草 提交于 2019-12-25 03:07:55

问题


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:


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



来源:https://stackoverflow.com/questions/22353969/drawing-straight-line-with-spritekit-and-uitouch-creates-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!