我正在开发一个带有精灵工具包的iOS应用程序,我用以下方式画一条线:
- (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 blackColor];
lineNode.lineWidth = 5.0f;
[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
{
CGPathRelease(pathToDraw);
}
这正是我想要的方式,但我不确定从下一步开始。
我想为刚刚绘制的线创建一个物理体。
我更喜欢touchesEnded方法。
如何将物理主体添加到线条中,无论其宽度如何?
我最好的猜测是跟踪所有点,然后做一些数学计算沿着线边缘的路径,然后添加物理体。
在我深入研究之前,还有更好的方法吗?