未调用 SpriteKit didBeginContact 函数

SpriteKit didBeginContact function not called

我正在尝试使用 didBeginContact 函数在我的 SpriteKit 游戏中进行碰撞。

我的问题是当球从砖块上弹开时根本没有调用该函数。这就是我设置它们的方式:

static const uint32_t blockCollisionCheck = 0x1 << 0;
static const uint32_t ballCollisionCheck = 0x1 << 1;

球:

SKShapeNode *ball = [[SKShapeNode alloc] init];

CGMutablePathRef drawPath = CGPathCreateMutable();
CGPathAddArc(drawPath, NULL, 0, 0, _ballRadius, 0, M_PI * 2, YES);
ball.path = drawPath;
CGPathRelease(drawPath);

ball.fillColor = [SKColor greenColor];

ball.position = CGPointMake(CGRectGetMidX(self.frame), 150);
ball.name = @"ball";


ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:_ballRadius];
ball.physicsBody.friction = 0.0;
ball.physicsBody.restitution = 1.0;
ball.physicsBody.linearDamping = 0.0f;
ball.physicsBody.allowsRotation = NO;
ball.physicsBody.dynamic = YES;
ball.physicsBody.categoryBitMask = ballCollisionCheck;
ball.physicsBody.contactTestBitMask = blockCollisionCheck;
[self addChild:ball];

积木:

SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(_blockWidth, _blockHeight)];
block.name = @"block";
block.position = CGPointMake(x, y);
block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:block.size];
block.physicsBody.allowsRotation = NO;
block.physicsBody.friction = 0.0;
block.physicsBody.dynamic = YES;
block.physicsBody.categoryBitMask = blockCollisionCheck;
block.physicsBody.contactTestBitMask = ballCollisionCheck;
[self addChild:block];

我这辈子都看不出这有什么问题,因为我认为类别位掩码是正确的?此外,这两个精灵都是动态的,这是我读到的另一个问题。

这并不是我的 didBeginContact 函数的内容不起作用,它只是从未到达那里,因为缺少 NSLog 消息和未达到断点。

如有任何帮助,我们将不胜感激。

谢谢。

如果像您所说的那样根本没有调用 didBeginContact 方法,我怀疑您没有将 self.physicsWorld.contactDelegate = self; 添加到您的 GameScene init 方法中。