使用Touch iOS Sprite Kit绘制线条 [英] Drawing line with Touch iOS Sprite Kit

查看:266
本文介绍了使用Touch iOS Sprite Kit绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在我的Sprite Kit场景中绘制线条,但没有任何显示.但是节点数增加了.任何人都可以看到问题所在.我一直在与这段代码作斗争

Im using the following code to draw lines in my sprite kit scene but nothing shows up. But the node counts goes up. Can anyone see what could be the problem. Ive been fighting with this code forever

- (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];
    //lineNode.lineWidth = 2;
    [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
{

  //  lineNode.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathToDraw];
  //  lineNode.physicsBody.dynamic = NO;
  //  lineNode.physicsBody.categoryBitMask = lines;
  //  lineNode.physicsBody.contactTestBitMask = 0;
  //  lineNode.physicsBody.collisionBitMask = blueParticles | redParticles| yellowParticles;

    CGPathRelease(pathToDraw);
}

编辑------删除背景节点后,代码将起作用.如何设置背景节点,以便可以覆盖顶部?谢谢

SKSpriteNode *background;

        if(screenHeight == 480){
            background = [SKSpriteNode spriteNodeWithImageNamed:@"iPhone4BG.png"];
        }
        if(screenHeight == 568){
            background = [SKSpriteNode spriteNodeWithImageNamed:@"iPhone5BG.png"];
        }
        if(screenHeight == 667){
            background = [SKSpriteNode spriteNodeWithImageNamed:@"iPhone6BG.png"];
        }
        if(screenHeight == 736){
            background = [SKSpriteNode spriteNodeWithImageNamed:@"iPhone6PlusBG.png"];
        }

        background.position = CGPointMake(screenWidth/2, screenHeight/2);
        background.size = CGSizeMake(screenWidth, screenHeight);

       // [self addChild:background];

推荐答案

注意核心问题是模拟器未呈现未关闭的路径.在设备上,此问题不存在.还涉及一个背景元素,原始海报没有提及,也没有用代码表示.该答案将解决模拟器上的问题.

NOTE Core issue was Simulator not rendering a path that isn't closed. On the device the issue doesn't exist. Also a background element was involved that the original poster didn't mention and wasn't represented in code. This answer will solve the issue on the simulator.

要解决您的问题,请按如下所示修改代码:

To fix your problem, modify your code as follows :

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
    // add this line to fix your issue
    CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
    lineNode.path = pathToDraw;

}

核心问题是,当渲染路径时,它没有完整的路径.您当前的方法是不断修改每次touchMove上的路径.

The core issue is that when it goes to render the path, it doesn't have a complete path. Your current approach is to be constantly modifying the path on each touchMove.

关闭子路径的方式是:

CGPathCloseSubpath(pathToDraw);

但是,每次将段添加到路径时,我选择仅使用CGPathMoveToPoint.

However I chose to just use CGPathMoveToPoint each time you added a segment to the path.

从CGPath参考获取CGPathMoveToPoint:

From CGPath reference for CGPathMoveToPoint :

此函数结束已经在进行中的子路径(如果有)并启动新的子路径,并在可选转换后将起点和当前点初始化到指定的位置(x,y)./em>

我要在当前子路径被渲染之前关闭它,并且还要设置下一个片段的开始位置.

I am closing the current subpath before it gets rendered, and also setting the start location for the next segment.

但是,如果要绘制一条长线或多条线,则会遇到性能严重问题,如注释中的链接中所定义.画一条很长的线,您会明白我的意思.通过最少的线条绘制,您可能会摆脱当前的方法.

However, you are going to run into major issues with performance as defined in the link in the the comments if you are going to be drawing a long line or many lines. Draw a really long line, and you will see what I mean. With minimal line drawing, you might get away with the current approach.

遗憾的是,使用SKShapeNode绘制的这种性质的图形没有达到应有的优化水平.

Sadly, drawing of this nature with SKShapeNode is not as optimized as it should be.

这篇关于使用Touch iOS Sprite Kit绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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