3

I have two SKSpriteNode first 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;
}

and second is 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];

And I am able to get collision detection by

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

Now my problem is every time hero jump and touch the coin it will go down to ground instead to continue jump and reach the height that it should, I know I am missing something here but don't know what it is, So anyone can please show me the right direction to correct this effect .

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

1 Answers1

0

Create an extra "nilCategory" and set the collisionBitMask of your coin..

coin.physicsBody.collisionBitMask = nilCategory;
user3138007
  • 637
  • 1
  • 6
  • 19