I wanna add a score when the character contacts with the 'coins' and game over when it contacts with the 'enemies'. But it doesn't work. I compiled the ...Category and <SKPhysicsContactDelegate> code in GameScene.h and these:
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.character = [SKSpriteNode spriteNodeWithImageNamed:@"character"];
self.character.position = CGPointMake(100, 100);
[self addChild:self.character];
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
}
return self;
}
in .m above didMoveToView method. Can I use initWithSize in Xcode 8? And I do use these code in .m:
character.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:character.size]; //1
character.physicsBody.dynamic = YES; //2
character.physicsBody.categoryBitMask = characterCategory; //3
character.physicsBody.contactTestBitMask = coinCategory | enemiesCategory;//4
character.physicsBody.collisionBitMask = 0; // 5
I use them in "setPhysicsBody:(SKPhysicsBody *)physicsBody" method. And of couse I compiled the didBeginContact method. Did I miss anything? Please help me. Thank you very much.