【问题标题】:SKPhysicsBody does not work on SKShapeNodeSKPhysicsBody 不适用于 SKShapeNode
【发布时间】:2014-03-06 16:11:38
【问题描述】:

我正在使用 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;

}

谢谢, 道格

【问题讨论】:

  • 路径是凸的,点是逆时针排列的吗?
  • @LearnCocos2D 你能详细说明一下吗?我在其他 SO 问题上阅读了您的类似建议,但不明白。代码在上面。路径是用户设置的任何内容。
  • 此处定义的凸面:en.wikipedia.org/wiki/Convex_and_concave_polygons 逆时针表示如果您查看维基百科页面上的五边形并从最上面的点开始,下一个点必须是右侧的那个,然后是右下角,然后是左下角和左侧。那是多边形的方向。复杂定义:en.wikipedia.org/wiki/…
  • @LearnCocos2D 您协助了一个非常相似的 SO 回复:stackoverflow.com/questions/20882339/… 但它似乎也不适用于 SO 成员。使用代码,你能给我一个按逆时针顺序设置路径凸点和点的例子吗?

标签: ios objective-c sprite-kit skshapenode


【解决方案1】:

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

【讨论】:

  • 谢谢。我在设置物理体之前添加 shapenode,如上面的代码所示。
  • 你在哪里做的?
  • 是的,就像我说的,尝试将新的 SKNode 添加到它的父节点并在添加它的物理体之前设置它的位置。
  • 谢谢迈克。我是 Spritekit 的新手,我错过了你所描述的概念。你能给我举个例子吗?在 touchesBegan 中添加 shapenode,然后在 touchesEnded 中添加物理体?
  • 另外,为了直接回答问题,我在touchesMoved中设置了节点的位置。
【解决方案2】:

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

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

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

selectorLine = SKPathNode(path: pathToDraw)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多