【发布时间】: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