【问题标题】:Drawing straight line with spritekit and UITouch creates multiple lines使用 spritekit 和 UITouch 绘制直线会创建多条线
【发布时间】:2014-03-12 14:03:05
【问题描述】:

应该很简单。

我正在尝试使用 UITouch 和 Spritekit 绘制一条直线。但是,当调用 touchesmoved 时,它会创建多行而不是单行。使用的代码如下:

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

    pathToDraw = CGPathCreateMutable();

    selectorLine = [SKShapeNode node];
    selectorLine.strokeColor = [SKColor greenColor];
    selectorLine.lineWidth = 5;
    [self addChild:selectorLine];
}

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

    CGPathMoveToPoint(pathToDraw2, NULL, positionInScene1.x, positionInScene1.y);
    CGPathAddLineToPoint(pathToDraw2, NULL, positionInScene2.x, positionInScene2.y);
    CGPathCloseSubpath(pathToDraw);

    selectorLine.path = pathToDraw;
}

如果我移动

CGPathAddLineToPoint(pathToDraw, NULL, positionInScene2.x, positionInScene2.y);

到 touchesEnd,它只在用户结束触摸后创建一行。我希望用户在触摸时看到正在绘制的线。

谢谢, 道格

【问题讨论】:

  • 多行从哪里到哪里?从起点到当前触摸位置的附加线,或前一个和当前触摸位置之间的附加线?

标签: ios objective-c sprite-kit uitouch


【解决方案1】:

在您的 touchesMoved 中创建一条新路径。您正在修改相同的路径,并且只是向其中添加越来越多的行。

【讨论】:

  • 不敢相信我错过了!谢谢泰斯。希望上面的代码可以帮助其他新手 sprtiekit 开发者
猜你喜欢
  • 1970-01-01
  • 2013-05-30
  • 2018-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2015-10-09
  • 2014-07-27
相关资源
最近更新 更多