使冲突在Apple SpriteKit中起作用

时间:2014-12-30 05:56:19

标签: ios sprite-kit collision-detection game-physics

我试图在Xcode中编写一个简单的应用程序 - fallDown游戏 - 我需要让球与平台发生碰撞。

首先 - 我尝试使用动画师并添加了对撞机和重力等但是我无法将框架设置为圆形(如果有人能告诉我那将会非常有用)。

第二 - 因为上面没有工作(与UIbezierpath有关)我决定使用SpriteKit。使用spriteKit可以创建对象/节点。球和平台是skshapenodes,我需要使用ball.physicsBody.collisionBitMask碰撞,但每次运行球都穿过身体

static const uint32_t ballCategory = 0x11 << 1;
static const uint32_t platformCategory = 0x11 << 3;

+ (SKNode *) spawnBall
{
    SKShapeNode *node = [[SKShapeNode alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, BALL_RADIUS, BALL_RADIUS)];

    node.path = [path CGPath];
    node.fillColor = [SKColor colorWithRed:0.7 green:0.0 blue:0.0 alpha:1.0];
    node.strokeColor = [SKColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    //node.glowWidth = BALL_GLOW_RADIUS;

    node.physicsBody.categoryBitMask = ballCategory;
    node.physicsBody.collisionBitMask = platformCategory;
    return node;
}
+ (NSMutableArray *) risingPlats
{
    // Screen Size
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGFloat width = screenBound.size.width;
    CGFloat height = screenBound.size.height;

    NSMutableArray *arrayOfShapes = [[NSMutableArray alloc] init];
    int numberPieces = arc4random() % 2;
    if (numberPieces) {
        SKShapeNode * node = [[SKShapeNode alloc] init];

        UIBezierPath *path = [UIBezierPath bezierPathWithRect:
                              CGRectMake((BALL_RADIUS*3-width)/2, -height, width-(BALL_RADIUS*3), BALL_RADIUS)];

        node.path = [path CGPath];
        node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
        node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

        node.physicsBody.categoryBitMask = platformCategory;
        node.physicsBody.collisionBitMask = ballCategory;

        [arrayOfShapes addObject:node];
    } else {
        int locationOfHole = (arc4random() % (int)(width-(BALL_RADIUS*6)))+BALL_RADIUS*1.5;

        // node1
        SKShapeNode * node = [[SKShapeNode alloc] init];
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:
                              CGRectMake((-width/2), -height, locationOfHole, BALL_RADIUS)];

        node.path = [path CGPath];
        node.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
        node.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

        node.physicsBody.categoryBitMask = platformCategory;
        node.physicsBody.collisionBitMask = ballCategory;

        [arrayOfShapes addObject:node];

        // node2
        SKShapeNode *node2 = [[SKShapeNode alloc] init];
        UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:
                              CGRectMake(locationOfHole+BALL_RADIUS*3-(width/2), -height, width-(locationOfHole+BALL_RADIUS*3), BALL_RADIUS)];

        node2.path = [path2 CGPath];
        node2.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:0.0 alpha:1.0];
        node2.strokeColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];

        node2.physicsBody.categoryBitMask = platformCategory;
        node2.physicsBody.collisionBitMask = ballCategory;

        [arrayOfShapes addObject:node2];
    }
    return arrayOfShapes;
}

我将上面的内容更改为contactbitmask,当我实现两个联系它的方法时,它从未打印到控制台。

1 个答案:

答案 0 :(得分:3)

你的节点没有分配给他们的物理实体。

你正在为他们分配物理学类别,但他们没有物理学家开始。这是零!

你需要做

yournode.physicsBody = [SKPhysicsBody <some initializer>]