问题
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