在精灵套件中画一条线

dev6546

我想在精灵套件中沿着touchesmoved收集的点画一条线。

最有效的方法是什么?我已经尝试了几次,但我的行要么在y轴上错了,要么占用了很多处理能力,每秒fps下降到10。

有任何想法吗?

成员们

您可以定义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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章