Manesh
Manesh

Reputation: 528

SpriteKit didBeginContact not being called

I hope you can help, have a block moving across and it jumps to avoid obstacles and collect coins. It's collision with the obstacles is working correctly and the below gameOver is working correctly.

-(void)didBeginContact:(SKPhysicsContact *)contact
{
    if ([contact.bodyA.node.name  isEqualToString:@"coins"] || [contact.bodyB.node.name  isEqualToString:@"coins"]) {
        [self coinCollected];   //THIS IS NOT WORKING
        NSLog(@"contacted"); //THIS IS NOT WORKING
    }
    else if ([contact.bodyA.node.name  isEqualToString:@"ground"] || [contact.bodyB.node.name  isEqualToString:@"ground"]) {
        [hero land];
    }
    else {
        NSLog (@"dead");
        [self gameOver];
            [self runAction:[SKAction playSoundFileNamed:@"gameover.wav" waitForCompletion:NO]];
    }
}

My PMWorldGenenerator file looks as below:

#import "PMWorldGenerator.h"
@interface PMWorldGenerator ()
@property double currentGroundX;
@property double currentObstacleX;
@property double coinX;
@property SKNode *world;

@end


@implementation PMWorldGenerator

static const uint32_t obstacleCategory = 0x1 << 1;
static const uint32_t groundCategory = 0x1 << 2;
static const uint32_t coinCategory = 0x1 << 3;

+ (id)generatorWithWorld:(SKNode *)world {
    PMWorldGenerator *generator = [PMWorldGenerator node];
    generator.currentGroundX = 0;
    generator.currentObstacleX  = 400;
    generator.coinX = 50;
    generator.world = world;
    return generator;
}

 -(void)populate
{
    for (int i = 0; i <3; i++)
        [self generate];
}


-(void)generate
{
    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width, self.scene.frame.size.height/2.7)];
        ground.name = @"ground";
    ground.position = CGPointMake(self.currentGroundX, -self.scene.frame.size.height/2 + ground.frame.size.height/2);
    ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
    ground.physicsBody.categoryBitMask = groundCategory;
    ground.physicsBody.dynamic = NO;
    [self.world addChild:ground];
    self.currentGroundX += ground.frame.size.width;

    SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10,10)];
        obstacle.name = @"obstacle";
    obstacle.position = CGPointMake(self.currentObstacleX/5, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 5);
    obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.size];
    obstacle.physicsBody.dynamic = NO;
    obstacle.physicsBody.categoryBitMask = obstacleCategory;
    [self.world addChild:obstacle];
    self.currentObstacleX += 550 ;

    SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
    coins.name = @"coins";
    coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);
    coins.physicsBody.categoryBitMask = coinCategory;
    coins.physicsBody.dynamic = YES;
    SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
    SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
    [coins runAction:repeatRotate];
    [self.world addChild:coins];
    self.coinX += 550;

}

and lastly my PMHero file:

#import "PMHero.h"
@interface PMHero ()
@end


@implementation PMHero
static const uint32_t heroCategory = 0x1 << 0;
static const uint32_t obstacleCategory = 0x1 << 1;
static const uint32_t groundCategory = 0x1 << 2;
static const uint32_t coinCategory = 0x1 << 3;


+(id)hero
{
    PMHero *hero = [PMHero spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(12,12)];
    hero.name = @"hero";
    hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hero.size];
    hero.physicsBody.categoryBitMask = heroCategory;
    hero.physicsBody.contactTestBitMask = obstacleCategory | groundCategory | coinCategory;
    return hero;
}

I have done exactly what I had done for my obstacles and ground for the "coins", but it doesn't detect any collisions with them in my didBeginContact

Upvotes: 0

Views: 444

Answers (1)

AppyMike
AppyMike

Reputation: 2064

You haven't added your physics body for coins

SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
coins.name = @"coins";
coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);

coins.physicsBody = [SKPhysicsBody bodyWith...//need code here

coins.physicsBody.categoryBitMask = coinCategory;
coins.physicsBody.dynamic = NO;
coins.physicsBody.collisionBitMask = 0;
SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
[coins runAction:repeatRotate];
[self.world addChild:coins];

Upvotes: 1

Related Questions