【问题标题】:SK Physics Body is not colliding with Edge Physics BodySK Physics Body 未与 Edge Physics Body 发生碰撞
【发布时间】:2014-05-28 17:27:58
【问题描述】:

我是 Sprite Kit 的新手,我正在尝试设置一个简单的游戏,让掉落的物体停留在屏幕范围内。但是,当我放下对象时,它并没有像应有的那样与屏幕边缘发生碰撞,而是直接掉了下来。

我设置了我的场景和它周围的边缘循环。

// Create and configure the scene.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:scene.frame];
scene.physicsWorld.gravity = CGVectorMake(0.0, -9.8);

这就是我为精灵画出我的物理身体的地方,精灵会在我触摸到的任何地方掉落。

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"SObstacle.png"];

    sprite.position = location;

    CGFloat offsetX = sprite.frame.size.width * sprite.anchorPoint.x;
    CGFloat offsetY = sprite.frame.size.height * sprite.anchorPoint.y;

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 50 - offsetX, 58 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 58 - offsetY);
    CGPathAddLineToPoint(path, NULL, 0 - offsetX, 32 - offsetY);
    CGPathAddLineToPoint(path, NULL, 24 - offsetX, 32 - offsetY);
    CGPathAddLineToPoint(path, NULL, 24 - offsetX, -2 - offsetY);
    CGPathAddLineToPoint(path, NULL, 80 - offsetX, -2 - offsetY);
    CGPathAddLineToPoint(path, NULL, 80 - offsetX, 24 - offsetY);
    CGPathAddLineToPoint(path, NULL, 50 - offsetX, 24 - offsetY);
    CGPathAddLineToPoint(path, NULL, 50 - offsetX, 56 - offsetY);

    CGPathCloseSubpath(path);

    sprite.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];

    [self addChild:sprite];

我已经用一个正方形的 SKSpriteNode 进行了尝试(我只使用了坐标而不是图像)并且效果很好。这可能只是一个新手错误,但我们感谢任何帮助。

【问题讨论】:

    标签: sprite-kit skphysicsbody


    【解决方案1】:

    您的路径轮廓定义了 concave shape 但多边形形状的主体 must use convex shapes

    您可以将路径更改为凸形,也可以将其拆分为多个通过关节连接的实体,以形成由单个凸形实体组成的更大的凹形。

    【讨论】:

    • 太棒了!感谢您清除它。有没有什么教程可以指点我,这样我就可以理解用单独的形状和关节创建一个更大的形状?我假设我会使用 bodyWithBodies?
    • 对,bodyWithBodies 但请记住,这仅适用于 ios 7.1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    相关资源
    最近更新 更多