我正在尝试使用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初始化方法中。
关于ios - SpriteKit didBeginContact函数未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29238830/