如何检测SKSpriteNode Contact但没有反应?

时间:2015-08-03 08:17:55

标签: ios objective-c skspritenode skphysicsbody skphysicscontact

我有两个SKSpriteNode第一个Hero

+(id)hero
{
    NSMutableArray *walkFrames = [NSMutableArray array];
    SKTextureAtlas *heroAnimatedAtlas = [SKTextureAtlas atlasNamed:@"HeroImages"];
    int numImages = (int)heroAnimatedAtlas.textureNames.count;
    for (int i=1; i <= numImages; i++) {
        NSString *textureName = [NSString stringWithFormat:@"hero%d", i];
        SKTexture *temp = [heroAnimatedAtlas textureNamed:textureName];
        [walkFrames addObject:temp];
    }
    Hero *hero = [Hero spriteNodeWithTexture:walkFrames[0]];
    hero.heroWalkingFrames = walkFrames;
    hero.name =@"Hero";
    hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
    hero.physicsBody.categoryBitMask = heroCategory;
    hero.physicsBody.categoryBitMask = obstacleCategory | groundCategory | homeCategory | ~goodiesCategory;

    return hero;
}

,其次是Coin

SKSpriteNode *coin = [SKSpriteNode spriteNodeWithImageNamed:@"coin"];
    coin.size =  CGSizeMake(10,10);
    coin.position = CGPointMake(100,100);
    coin.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:coin.size];
    coin.physicsBody.contactTestBitMask = coinCategory;
    coin.physicsBody.dynamic=NO;
    coin.name = @"coin";

    [self.world addChild:coin];

我能够通过

进行碰撞检测
if([contact.bodyA.node.name  isEqual: @"coin"] || [contact.bodyB.node.name  isEqual: @"coin"])
    {
        //[self LevelComplete];
        SKNode* coinNode ;
        if ([contact.bodyA.node.name  isEqual: @"coin"]) {
            coinNode=contact.bodyA.node;
        }
        else{
            coinNode=contact.bodyB.node;
        }

        [coinNode removeFromParent];
        NSLog(@"Coin touched");
    }

现在我的问题是,每次英雄跳跃并触摸硬币它会下降到地面而不是继续跳跃并达到它应该的高度,我知道我在这里遗漏了一些但不知道它是什么,所以任何人都可以告诉我正确的方向来纠正这种影响。

1 个答案:

答案 0 :(得分:0)

创建一个额外的“nilCategory”并设置硬币的collisionBitMask ..

coin.physicsBody.collisionBitMask = nilCategory;