0

我使用以下代码在我的精灵套件场景中绘制线条,但没有显示任何内容。但是节点数增加了。任何人都可以看到可能是什么问题。我一直在与这段代码作斗争

- (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];
4

2 回答 2

2

我认为这就是你想要实现的。它会在您移动手指时绘制一条临时线(白色),并在您抬起手指时绘制一条最终线(红色)。您将需要声明一个名为startingPoint 的CGPoint 实例变量并删除名为pathToDraw 的CGMutablePathRef ivar。也可以修改代码以绘制多个连接的线段。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    startingPoint = positionInScene;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    // Remove temporary line if it exist
    [lineNode removeFromParent];

    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    //CGPathRelease(pathToDraw);
    lineNode.strokeColor = [SKColor whiteColor];
    lineNode.lineWidth = 1;
    [self addChild:lineNode];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    // Remove temporary line
    [lineNode removeFromParent];

    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    SKShapeNode *finalLineNode = [SKShapeNode node];
    finalLineNode.path = pathToDraw;
    //CGPathRelease(pathToDraw);
    finalLineNode.strokeColor = [SKColor redColor];
    finalLineNode.lineWidth = 1;
    [self addChild:finalLineNode];
}
于 2014-09-25T18:35:48.523 回答
2

注意核心问题是模拟器未渲染未关闭的路径。在设备上,问题不存在。还涉及到原始海报未提及且未在代码中表示的背景元素。这个答案将解决模拟器上的问题。

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

- (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 上的路径。

关闭子路径的方式是:

CGPathCloseSubpath(pathToDraw);

但是,我选择只在CGPathMoveToPoint每次向路径添加段时使用。

来自 CGPath 参考CGPathMoveToPoint

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

我在渲染之前关闭当前子路径,并设置下一段的起始位置。

但是,如果您要绘制一条长线或多条线,您将遇到评论中链接中定义的主要性能问题。画一条很长的线,你就会明白我的意思。使用最少的线条绘制,您可能会摆脱当前的方法。

可悲的是,这种性质的绘图SKShapeNode并没有得到应有的优化。

于 2014-09-25T19:23:39.013 回答