【问题标题】:draw a line in sprite kit in touchesmoved在 touchesmoved 的 sprite kit 中画一条线
【发布时间】:2013-11-13 12:29:58
【问题描述】:

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

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

有什么想法吗?

【问题讨论】:

    标签: ios7 sprite-kit touchesmoved


    【解决方案1】:

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

    【讨论】:

    • 出于好奇,如果你画 10 秒,帧率性能如何?它是否开始退化?
    • 在我的实现中,帧速率保持不变。
    • 什么都没有显示给我知道为什么吗?节点数增加,但在绘制时什么都看不到我什至注释掉最后几行以保留该行
    • 您是在课堂上实现代码还是单独尝试?
    猜你喜欢
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多