【问题标题】:How to create a path for a labyrinth with physics body in SpriteKit如何在 SpriteKit 中为带有物理体的迷宫创建路径
【发布时间】:2015-07-29 11:02:40
【问题描述】:

我想为我的 SpriteKit 游戏创建一个迷宫,但我在设置正确的物理实体时遇到了问题。看看这个例子:

蓝色:CGPath

绿色:迷宫(CGPath linewidth=10)

红色:我要创建的物理体

我尝试使用 CGPath 和 SKShapeNode 绘制迷宫,但是如何为迷宫的内部边界设置物理体?

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 38, 266);
CGPathAddLineToPoint(path, NULL, 37, 23);
CGPathAddLineToPoint(path, NULL, 127, 22);
CGPathAddLineToPoint(path, NULL, 127, 184);
CGPathAddLineToPoint(path, NULL, 273, 185);
CGPathAddLineToPoint(path, NULL, 273, 98);
CGPathAddLineToPoint(path, NULL, 274, 184);
CGPathAddLineToPoint(path, NULL, 127, 184);
CGPathAddLineToPoint(path, NULL, 127, 23);
CGPathAddLineToPoint(path, NULL, 364, 23);
CGPathAddLineToPoint(path, NULL, 364, 266);
CGPathCloseSubpath(path);
SKShapeNode *labyrinth = [SKShapeNode shapeNodeWithPath:path];
labyrinth = [SKColor greenColor];
labyrinth.lineWidth = 10;
[self addChild:labyrinth];

labyrinth.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:path];

不起作用。我需要一个选项来包含物理体路径的线宽。

有任何建议或其他选项来实现这样的迷宫吗?

【问题讨论】:

    标签: ios macos sprite-kit skphysicsbody cgpath


    【解决方案1】:

    您是否考虑过用矩形构建迷宫?

    每个迷宫墙可以是一个矩形形状或精灵节点,然后您可以使用bodyWithRectangleOfSize: 为每个形状/精灵节点赋予一个与其自身大小相同的物理体。

    【讨论】:

      【解决方案2】:

      感谢@primaryartemis!根据您的建议,我创建了以下代码:

              for (int i=0; i< [points_array count]-1; i++) {
                    // first point
                    CGPoint p1 = CGPointMake(points_array[i].x, points_array[i].y);
                    // second point                  
                    CGPoint p2 = CGPointMake(points_array[i+1].x, points_array[i+1].y);
                    // midpoint
                    CGPoint p3 = CGPointMake(p1.x-p2.x, p1.y-p2.y);
      
                    float angle = atanf(p3.y/p3.x);
                    NSInteger length = sqrt(p3.x*p3.x + p3.y*p3.y);
      
                    SKSpriteNode *frameElement = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(length+10, 10)];
                    frameElement.zRotation = angle;
                    frameElement.position = CGPointMake(p1.x-p3.x/2, p1.y-p3.y/2);
      
                    frameElement.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:frameElement.size];
                    [self addChild:frameElement];
              }
      

      它有效。这是一个示例输出: (红色为物理体,蓝色为中点)

      【讨论】:

        【解决方案3】:

        如果您不花费数小时翻译代码,只需使用来自您的 SKShapeNode 的最终图像创建一个主体。

        您的节点必须在视图中,因为您将使用名为 textureFromNode 的 SKView 方法:如下所示。

        if(labyrinth.scene.view)
        {
            SKTexture *texture = [labyrinth.scene.view textureFromNode: labyrinth];
        
            labyrinth.physicsBody = [SKPhysicsBody bodyWithTexture:texture alphaThreshold:0.0 size:texture.size];
            labyrinth.physicsBody.dynamic = NO;
        }
        

        【讨论】:

        • 所以 SKShapeNode 迷宫可以是 png 图像吗?怎么样?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-24
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 2011-01-26
        相关资源
        最近更新 更多