我正在使用SpriteKit开发游戏。在我的游戏中,玩家应该能够绘制一条线并让它与之相互作用。我使用了SKShapeNode
绘制的简单CGMutablePathRef
。但是当我将physicsBody添加到该行时,它会自动将该行的末尾重新连接到开头。结果是,如果用户绘制曲线,则physicsBody是半圆形。
- (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路径重新回到开头。任何帮助,将不胜感激。谢谢!
答案 0 :(得分:2)
当您在屏幕上移动手指时,以下代码会绘制一条临时线(白色),然后在抬起手指时绘制最后一条线(红色)。然后代码将边缘链物理主体添加到该行。
@implementation GameScene {
SKShapeNode *lineNode;
CGPoint startingPoint;
}
-(void)didMoveToView:(SKView *)view {
self.scaleMode = SKSceneScaleModeResizeFill;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
startingPoint = positionInScene;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
// Remove temporary line if it exist
[lineNode removeFromParent];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.strokeColor = [SKColor whiteColor];
lineNode.lineWidth = 1;
[self addChild:lineNode];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
// Remove temporary line
[lineNode removeFromParent];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
SKShapeNode *finalLineNode = [SKShapeNode node];
finalLineNode.path = pathToDraw;
finalLineNode.strokeColor = [SKColor redColor];
finalLineNode.lineWidth = 1;
finalLineNode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:pathToDraw];
[self addChild:finalLineNode];
}
@end