在touchesmoved中用sprite工具包画一条线 [英] draw a line in sprite kit in touchesmoved

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

问题描述

我想沿着touchesmoved中收集的点在sprite工具包中画一条线。

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.

任何想法?

推荐答案

您可以定义CG路径并通过在触摸移动功能中添加线条或弧线来修改它。之后,您可以从路径创建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工具包画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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