SKPhysicsBody不适用于SKShapeNode

时间:2014-03-06 16:11:39

标签: ios objective-c sprite-kit skshapenode

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

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

之前有没有遇到过这个?你如何成功地将物理体添加到动态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;

}

谢谢, 道格

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我找到了解决它的解决方案(在Swift中)。我遇到了与你面临的完全相同的问题。而不是像

那样设置节点路径
selectorLine.path = pathToDraw

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

selectorLine = SKPathNode(path: pathToDraw)