我有一个简单的跳跃游戏设置,英雄跳过一个滚向他的球。
现场没有重力设置
英雄直接向上跳,然后直接向下跳。如果英雄保持静止" didBeginContact"被解雇了,我可以发现碰撞。如果英雄的前部在跳起或下降时击中滚球,那么就会开始接触#34; doBeginContact"被解雇了。但是如果英雄的后半部分落在球的下方,它就不会发现碰撞。有没有人知道它为什么只能抓住前方的碰撞?这似乎更像边缘检测与对象检测!那可能吗?
static const uint32_t groundCategory = 0x1 << 1;
static const uint32_t obstacleCategory = 0x1 << 2;
static const uint32_t heroCategory = 0x1 << 3;
SKSpriteNode *hero = [[SKSpriteNode alloc] initWithColor:[SKColor redColor] size:CGSizeMake(100, 115)];
[hero setPosition:CGPointMake(HERO_UP_POSITION_X, HEIGHT_GAME / 2 + hero.size.height / 2 + 10)];
hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
[hero.physicsBody setAffectedByGravity:NO];
hero.physicsBody.usesPreciseCollisionDetection = YES;
hero.physicsBody.allowsRotation = NO;
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.contactTestBitMask = obstacleCategory | groundCategory;
hero.physicsBody.collisionBitMask = 0;
hero.zPosition = 0;
hero.name = kHeroUpName;
[self addChild:hero];
obstacle = [[SKSpriteNode alloc] initWithImageNamed:kImgObstacleDown];
obstacle.name = @"obstacle";
[obstacle setPosition:CGPointMake(WIDTH_GAME + x, HEIGHT_GAME / 2 + y)];
obstacle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:obstacle.size.width / 2];
obstacle.physicsBody.usesPreciseCollisionDetection = YES;
[obstacle.physicsBody setAffectedByGravity:NO];
obstacle.physicsBody.categoryBitMask = obstacleCategory;
obstacle.physicsBody.contactTestBitMask = heroCategory;
obstacle.physicsBody.collisionBitMask = 0;
obstacle.zPosition = 1;
答案 0 :(得分:1)
我弄明白了这个问题。
我正在检查对象是否超过了英雄以便跟踪得分,如果它已超过英雄的中心我重命名它,以便它不会被检查并再次得分。
谢谢@ 0x141E你的片段让我意识到我正在检查名字inDidBeginContact(失败)与检查categoryBitMask
答案 1 :(得分:0)
有两个测试用例需要在你的didBeginContact中:1)如果contact.bodyA是英雄而contact.bodyB是障碍,2)如果contact.bodyA是障碍,contact.bodyB是英雄。您可能只处理一个案例。以下是如何执行此操作的示例:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if ((contact.bodyA.categoryBitMask == heroCategory && contact.bodyB.categoryBitMask obstacleCategory)
|| (contact.bodyA.categoryBitMask == obstacleCategory && contact.bodyB.categoryBitMask == heroCategory)) {
// Do something here
}
}