我无法在开始接触时停止节点

时间:2014-08-25 15:42:51

标签: ios objective-c sprite-kit

我尝试了很多方法,但在开始接触时我并没有阻止猫。这是节点移动方法:

[self enumerateChildNodesWithName:@"cat" usingBlock:^(SKNode *node, BOOL *stop){
        SKSpriteNode *tempCat = (SKSpriteNode *) node;
        float actionDuration = 0.1;
        CGPoint offset = CGPointSubtract(_zombie.position, tempCat.position);
        CGPoint direction = CGPointNormalize(offset);
        CGPoint amountToMovePerSec = CGPointMultiplyScalar(direction, CAT_MOVE_POINTS_PER_SEC);
        CGPoint amountToMove = CGPointMultiplyScalar(amountToMovePerSec, actionDuration);
        node.position = CGPointMake(node.position.x+amountToMove.x, node.position.y+amountToMove.y);
        _velocityCat = CGPointMultiplyScalar(direction, CAT_MOVE_POINTS_PER_SEC);

        [self rotateNode:tempCat toFace:_velocityCat rotateRadiansPerSec:CAT_ROTATE_RADIANS_PER_SEC];

开始接触时如何阻止猫?

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

    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (enemyCategory | catsCategory)) {
        [self enumerateChildNodesWithName:@"cat" usingBlock:^(SKNode *node, BOOL *stop) {
            if ([contact.bodyA.node isEqual:node] || [contact.bodyB.node isEqual:node])
            {
                node.physicsBody = NO;
                SKAction *wait = [SKAction waitForDuration:2];
                SKAction *catDie = [SKAction runBlock:^{
                    [node removeFromParent];
                }];
                SKAction *animation = [SKAction sequence:@[wait, catDie]];
                [self runAction:animation];
            }
        }];        NSLog(@"test 1");
    }

1 个答案:

答案 0 :(得分:0)

不需要枚举所有cat节点,因为您可以从SKPhysicsContact对象获取联系人所涉及的节点。以下显示了如何做到这一点......

编辑:您可以更改猫节点的名称以防止它追逐僵尸

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

    if (collision == (enemyCategory | catsCategory)) {
        SKNode *node = contact.bodyA.node;
        if (contact.bodyB.categoryBitMask == catsCategory) {
            node = contact.bodyB.node;
        }
        // One way to stop the cat from chasing zombie is by changing its name
        node.name = @"not cat";
    }
}