首页 文章

使冲突在Apple SpriteKit中起作用

提问于
浏览
0

我试图在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 回答

  • 3

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

    你正在为他们分配物理学类别,但他们没有物理学家开始 . 没关系!

    你需要做的

    yournode.physicsBody = [SKPhysicsBody <some initializer>]
    

相关问题