在 touchesmoved 的 sprite kit 中画一条线 [英] draw a line in sprite kit in touchesmoved

查看:13
本文介绍了在 touchesmoved 的 sprite kit 中画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 sprite kit 中沿着 touchesmoved 中收集的点画一条线.

I would like to draw a line in sprite kit along the points collected in touchesmoved.

这样做最有效的方法是什么?我尝试了几次,我的线要么在 y 轴上出错,要么占用了大量的处理能力,导致 fps 下降到每秒 10 次.

Whats the most efficient way of doing this? I've tried a few times and my line is either wrong on the y axis, or takes up a lot of processing power that the fps goes down to 10 a second.

有什么想法吗?

推荐答案

您可以定义一个 CGpath 并通过在您的触摸移动函数中添加线条或圆弧来修改它.之后,您可以从您的路径创建一个 SKShapeNode 并根据需要对其进行配置.如果你想在手指在屏幕上移动的时候画线,你可以在触摸开始时创建形状节点,然后修改它.

You could define a CGpath and modify it by adding lines or arcs in your touch moved function. After that, you can create a SKShapeNode from your path and configure it as you prefer. If you want to draw the line while the finger is moving on the screen you can create the shape node when the touch begins with an empty path and then modify it.

我写了一些代码,它对我有用,画了一条简单的红线.

I wrote some code, it works for me, draws a simple red line.

在您的 MyScene.m 中:

In your 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

这篇关于在 touchesmoved 的 sprite kit 中画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆