0

我正在使用 UITouch 绘制一个 ShapeNode,如下所示:

线条正确绘制。但是,我的问题是将物理体添加到 shapenode(我希望 SKSpriteNode 碰撞和反弹)。但就好像物理体从未附加到 shapenode 上一样。我使用 YMCPhysicsDebugger 来查看物理体相对于 shapenode 的位置,但它没有检测到任何东西。

有没有人遇到过这个?您如何成功地将物理体添加到动态形状节点?

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

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

    selectorLine = [SKShapeNode node];
    selectorLine.path = pathToDraw;
    selectorLine.strokeColor = [SKColor greenColor];
    selectorLine.lineWidth = 10;
    selectorLine.physicsBody=[SKPhysicsBody bodyWithPolygonFromPath:pathToDraw];
    selectorLine.physicsBody.restitution=1.0f;
    selectorLine.physicsBody.dynamic=YES;
    selectorLine.physicsBody.usesPreciseCollisionDetection=YES;
    selectorLine.physicsBody.mass=0.2;
    selectorLine.zPosition=1.5;

    [self addChild:selectorLine];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

 touch = [touches anyObject];
 CGPoint positionInScene = [touch locationInNode:self];
 CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
 selectorLine.path = pathToDraw;

}

谢谢,道格

4

2 回答 2

1

我记得读过你需要SKNode在添加物理体之前将其添加到它的父级并设置它的位置......

于 2014-03-06T16:30:11.067 回答
1

我找到了为我解决它的解决方案(在 Swift 中)。我遇到了与您面临的完全相同的问题。而不是像这样设置节点路径

selectorLine.path = pathToDraw

尝试在构造函数中设置它而不是像

selectorLine = SKPathNode(path: pathToDraw)
于 2015-10-06T21:54:22.297 回答