2013-10-20 2 views
2

단일 충돌에 대한 응답을 찾았지만 여러 가지 유형의 충돌을 감지 할 방법을 찾고 있습니다. 나는 내가 원하는 3 가지 충돌이있는 게임을 만들고있다. 사용자의 비행기가 적의 총알과 충돌하고, 사용자의 총알이 적 비행기와 충돌하며, 적의 총알과 사용자 총알이 충돌합니다. 모든 카테고리 BitMask 및 contactTestBitMask가 설정되고 수정되었습니다. 여기에 내 대리자 메서드입니다.여러 충돌에 대한 스프라이트 키트 충돌

- (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]; 
} 
} 

답변

5

이 그것을 작동합니다 이미 테스트

//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]; 
    } 
} 

행운을 일하고있어!

관련 문제