【发布时间】:2013-11-13 12:29:58
【问题描述】:
我想在 sprite kit 中沿着 touchesmoved 中收集的点画一条线。
这样做最有效的方法是什么?我尝试了几次,我的线要么在 y 轴上出错,要么占用了大量的处理能力,导致 fps 下降到每秒 10 次。
有什么想法吗?
【问题讨论】:
标签: ios7 sprite-kit touchesmoved
我想在 sprite kit 中沿着 touchesmoved 中收集的点画一条线。
这样做最有效的方法是什么?我尝试了几次,我的线要么在 y 轴上出错,要么占用了大量的处理能力,导致 fps 下降到每秒 10 次。
有什么想法吗?
【问题讨论】:
标签: ios7 sprite-kit touchesmoved
您可以定义一个 CGpath 并通过在您的触摸移动功能中添加线或弧来修改它。之后,您可以从您的路径创建一个 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
【讨论】: