我正在使用UITouch繪製一個ShapeNode,如下所示:SKPhysicsBody無法在SKShapeNode上工作
該線條正確繪製。然而,我的問題是將物理體添加到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;
}
感謝, 道格
在構造函數中設置它的節點路徑是路徑凸是逆時針的順序點? – LearnCocos2D
@ LearnCocos2D你能否詳細說明一下?我從其他SO問題上閱讀了類似的建議,但不明白。代碼在上面。路徑是用戶設置的任何內容。 – Doug
此處定義的凸:http://en.wikipedia.org/wiki/Convex_and_concave_polygons逆時針意味着,如果您在維基百科頁面上查看五角形形狀,並從最高點開始,則下一個點必須是右側,然後右下角,然後左下角和左側。這是多邊形的方向。複雜的定義:http://en.wikipedia.org/wiki/Curve_orientation#Orientation_of_a_simple_polygon – LearnCocos2D