xcode - SpriteKit 物理线的主体

标签 xcode sprite-kit

我正在使用 SpriteKit 开发游戏。在我的游戏中,玩家应该能够画一条线并让事物与其交互。我使用的是用 CGMutablePathRef 绘制的简单 SKShapeNode。但是,当我将物理主体添加到该行时,它会自动将行的末尾重新连接到开头。结果是,如果用户绘制一条曲线,physicalBody 就是半圆形。

- (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];
}

我似乎无法弄清楚如何防止物理主体路径重新回到开头。任何帮助,将不胜感激。谢谢!

最佳答案

以下代码会在您在屏幕上移动手指时绘制一条临时线(白色),然后在您抬起手指时绘制最后一条线(红色)。然后,代码将边缘链物理体添加到线上。

@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

关于xcode - SpriteKit 物理线的主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074220/

相关文章:

ios - 无法将应用程序部署到 ios 8 设备

Swift - Init 方法和随机化不起作用

swift - 在 swift spritekit 中更改定时器的时间间隔

swift - Swift 3 中的 Depreciation++ 运算符,寻找解决方法

ios - 如何在swift 2中设置随机颜色的计时器?

ios - XCode 7 上的 libswiftSecurity.dylib 为 "No such file or directory (2)"

Ios - 识别二维码响应类型

SwiftUI 将保存的值传递给父 View

ios - 仅当某些特定的 iOS Swift 代码在 Debug模式下运行时,我如何才能运行它?

objective-c - SKShapeNode有时会在dealloc EXC_BAD_ACCESS上产生崩溃