2015-08-18 38 views
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路徑重新回到開始。任何幫助,將不勝感激。謝謝!

回答

1

當您在屏幕上移動手指時,下面的代碼繪製一條臨時線條(白色),然後在您擡起手指時畫出最後一行(紅色)。代碼然後添加一個邊緣鏈物理體到線上。

@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 

enter image description here