3

我有两个 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;
}

第二个是硬币

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");
    }

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

4

1 回答 1

0

创建一个额外的“nilCategory”并设置你的硬币的碰撞位掩码..

coin.physicsBody.collisionBitMask = nilCategory;
于 2015-09-04T20:28:19.980 回答