1
我正在開發一款SpriteKit遊戲。在我的遊戲中,玩家應該能夠畫出一條線並讓事物與它交互。我正在使用與CGMutablePathRef
一起繪製的簡單SKShapeNode
。但是,當我將該物理體添加到該行時,它會自動將該行的末尾重新連接到開頭。結果是,如果用戶繪製曲線,那麼physicsBody就是半圓的形狀。SpriteKit physics物理線路
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKShapeNode *line = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, location.x, location.y);
line.path = pathToDraw;
[line setStrokeColor:[UIColor redColor]];
[line setLineWidth:5];
[line setLineCap:kCGLineCapRound];
[line setLineJoin:kCGLineJoinRound];
line.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathToDraw];
line.physicsBody.dynamic = NO;
line.physicsBody.restitution = 1;
line.name = @"line";
[self addChild:line];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
SKShapeNode *oldLine = (SKShapeNode *)[self childNodeWithName:@"line"];
CGMutablePathRef pathToDraw = (CGMutablePathRef)oldLine.path;
CGPathAddLineToPoint(pathToDraw, NULL, location.x, location.y);
[self enumerateChildNodesWithName:@"line" usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
SKShapeNode *line = [SKShapeNode node];
line.path = pathToDraw;
[line setStrokeColor:[UIColor redColor]];
[line setLineWidth:5];
[line setLineCap:kCGLineCapRound];
[line setLineJoin:kCGLineJoinRound];
line.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:pathToDraw];
line.physicsBody.dynamic = NO;
line.physicsBody.restitution = 1;
line.name = @"line";
[self addChild:line];
}
我似乎無法弄清楚如何防止physicsBody路徑重新回到開始。任何幫助,將不勝感激。謝謝!