ios - 多次碰撞的 Sprite Kit 碰撞

标签 ios objective-c collision

我已经查看并找到了单个碰撞的答案,但我正在寻找一种方法来检测不止一种类型的碰撞。我正在制作一个游戏,其中有 3 次我想要的碰撞。用户飞机与敌人的子弹相撞,用户的子弹与敌机相撞(我已经在工作),以及敌人的子弹和用户的子弹相撞。我已设置并更正所有 categoryBitMask 和 contactTestBitMask。这是我的委托(delegate)方法。

 - (void) didBeginContact:(SKPhysicsContact *)contact {

SKPhysicsBody *firstBody, *secondBody;


if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
}
else
{
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}

// if user plane hits enemy bullet
if ((firstBody.categoryBitMask == playerShipCategory) &&
    (secondBody.categoryBitMask == enemyBulletCategory)) {

    [self takeDamageByAmount:POINT_INCREMENTER];
    [_enemyBullet removeFromParent];
    SKAction *bounce = [SKAction sequence:@[
                                            [SKAction fadeAlphaTo:.5 duration:.2],
                                            [SKAction fadeAlphaTo:1.0 duration:.2],
                                            [SKAction fadeAlphaTo:.5 duration:.2],
                                            [SKAction fadeAlphaTo:1.0 duration:.2]
                                            ]];
    [_playerPlane runAction:bounce];
}

// if the user bullet hits the enemy bullet
else if ((firstBody.categoryBitMask == bulletCategory) &&
   (secondBody.categoryBitMask == enemyBulletCategory)) {
    [_enemyBullet removeFromParent];
    [_bullet removeFromParent];
}

// if bullet hits enemy ship - THIS ONE WORKS, but none of the others work for some reason
else if ((firstBody.categoryBitMask == bulletCategory) &&
    (secondBody.categoryBitMask == enemyShipCategory)) {

    [self gainPointsByAmoint:POINT_INCREMENTER];
    [self projectile:(SKSpriteNode *)firstBody.node didCollideWithMonster:(SKSpriteNode *)secondBody.node];
}
}

最佳答案

这应该可以工作,它已经过测试并且可以工作

//Define the collider Category

  typedef NS_ENUM(uint32_t, CollisionType) {
    enemyShipCategory        = 0x1 << 0,
    enemyBulletCategory      = 0x1 << 1,
    playerShipCategory       = 0x1 << 2,
    bulletCategory           = 0x1 << 3,
  };


// Set the category that this physics body belongs to 
// and specify which categories of bodies cause intersection 
// notifications with this physics body

  ship.physicsBody.categoryBitMask = playerShipCategory;       
  ship.physicsBody.contactTestBitMask = enemyBulletCategory;

  shipBullet.physicsBody.categoryBitMask = bulletCategory;
  shipBullet.physicsBody.contactTestBitMask = enemyShipCategory | enemyBulletCategory;

  enemy.physicsBody.categoryBitMask = enemyShipCategory;
  enemy.physicsBody.contactTestBitMask = bulletCategory;

  enemyBullet.PhysicsBody.categoryBitMask = enemyBulletCategory;
  enemyBullet.physicsBody.contactTestBitMask = bulletCategory;

// And handle Collisions

- (void)didBeginContact:(SKPhysicsContact *)contact {
  uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

  if (collision == (playerShipCategory | enemyBulletCategory)) {
    SKNode *Ship, *bullet;

    if (contact.bodyA.categoryBitMask == playerShipCategory) {
      Ship = contact.bodyA.node;
      bullet = contact.bodyB.node;
    } else {
      Ship = contact.bodyB.node;
      bullet = contact.bodyA.node;
    }

    [self takeDamageByAmount:POINT_INCREMENTER];
    [bullet removeFromParent];

    SKAction *bounce = [SKAction sequence:@[
      [SKAction fadeAlphaTo:.5 duration:.2],
      [SKAction fadeAlphaTo:1.0 duration:.2],
      [SKAction fadeAlphaTo:.5 duration:.2],
      [SKAction fadeAlphaTo:1.0 duration:.2]
    ]];

    [Ship runAction:bounce];
  }

  else if (collision == (bulletCategory | enemyBulletCategory)) {
    [contact.bodyA.node removeFromParent];
    [contact.bodyB.node removeFromParent];
  }

  else if (collision == (bulletCategory | enemyShipCategory)) {
    SKNode *shipBullet, *enemyShip;

    if (contact.bodyA.categoryBitMask == shipBullet) {
      shipBullet = contact.bodyA.node;
      enemyShip = contact.bodyB.node;
    } else {
      shipBullet = contact.bodyB.node;
      enemyShip = contact.bodyA.node;
    }

    [self gainPointsByAmoint:POINT_INCREMENTER];
    [self projectile:shipBullet didCollideWithMonster:enemyShip];
  }
}

祝你好运!

关于ios - 多次碰撞的 Sprite Kit 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473598/

相关文章:

ios - 如何将图像存储在核心数据中

javascript - 碰撞响应问题——矩形内的基本圆

objective-c - 在滑动之前允许延迟

objective-c - 如何像 Twitter 一样自定义 UITableViewCell?

ios - 如何获取iPhone中所有视频文件的列表

ios - UILabel 阴影已被意外切断

java - 不同角色速度的 Pacman 碰撞检测?

java - 使用另一个哈希函数处理冲突?

iphone - XCode 4 - 通用应用程序 - 使用选项卡栏 Controller 时 View 不会改变

iphone - 清除委托(delegate)