Found the answer to my problem... Dynamic was set to NO for the power up token object. This disabled spritekit from checking for collision detection on this object.
powerToken.physicsBody.dynamic = NO;
質問
didbegincontact is not being called for powerup token - other spritenodes work fine. For instance player (plane) contacts helicopter works fine. Player (plane) contacts power up token and nothing happens (no didbegincontact) call. I have changed the masks to the same as helicopter but it still doesn't work. Is there a limit to the number of node category masks or something? I have been stuck on this a few days now.
//zpositions
float playerZ = .08;
float enemyZ = .08;
float explosionZ = .08;
float tokenz = .08;
//category bit masks
#define playermask 1
#define enemymask 2
#define bulletmask 3
#define tokenmask 9
//add plane
SKSpriteNode *plane = [SKSpriteNode spriteNodeWithImageNamed:@"jet"];
plane.position = CGPointMake(160, 80);
plane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:plane.size];
//plane.size = CGSizeMake(15,25);
plane.physicsBody.dynamic = NO;
plane.physicsBody.affectedByGravity = NO;
//collision configuration - need to fine tune later
plane.physicsBody.categoryBitMask = playermask;
plane.physicsBody.collisionBitMask = playermask | enemymask | tokenmask;
plane.physicsBody.contactTestBitMask = playermask;
plane.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:plane];
plane.zPosition = playerZ;
player = plane;
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;
//////// TESTING TESTING TESTING ----- Delete After
[self powerUp];
temp.position = CGPointMake(160, 160);
SKAction *makeHelis = [SKAction sequence:@[[SKAction waitForDuration:1.50 withRange:0.35],
[SKAction performSelector:@selector(helicopterEnemies) onTarget:self]]];
[self runAction: [SKAction repeatActionForever:makeHelis]];
//method call for spawning power up tokens
SKAction *makeTokens = [SKAction sequence:@[[SKAction waitForDuration:25.50 withRange:0.35],
[SKAction performSelector:@selector(powerUp) onTarget:self]]];
[self runAction: [SKAction repeatActionForever:makeTokens]];
- (SKSpriteNode*)helicopterEnemies
{
//Helicopter setup
SKSpriteNode *heli = [SKSpriteNode spriteNodeWithImageNamed:@"heli"];
heli.size = CGSizeMake(18,18);
heli.position = CGPointMake(skRand(100, 250),
self.size.height-50);
heli.name = @"helicopter";
heli.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:heli.size];
//need to work on these numbers to coordinate with the other sprites
heli.physicsBody.categoryBitMask = enemymask;
heli.physicsBody.collisionBitMask = enemymask | bulletmask;
heli.physicsBody.contactTestBitMask = bulletmask;
heli.physicsBody.usesPreciseCollisionDetection = YES;
heli.physicsBody.dynamic = YES;
heli.physicsBody.affectedByGravity = NO;
//useless except to show you could change this
//heli.physicsBody.affectedByGravity = YES;
[self addChild:heli];
SKAction *moveDownThenDie = [SKAction sequence:@[
[SKAction waitForDuration:0.10 withRange:0.35],
[SKAction moveByX:0.0 y:-1600 duration:25],
[SKAction removeFromParent]]];
heli.zPosition = enemyZ;
[heli runAction: moveDownThenDie];
return heli;
}
- (SKSpriteNode*)powerUp
{
//Power token setup
SKSpriteNode *powerToken = [SKSpriteNode spriteNodeWithImageNamed:@"powerup"];
//powerToken.size = CGSizeMake(18,18);
powerToken.position = CGPointMake(skRand(100, 250),
self.size.height-50);
powerToken.name = @"Token";
powerToken.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:powerToken.size];
//need to work on these numbers to coordinate with the other sprites
powerToken.physicsBody.categoryBitMask = tokenmask;
powerToken.physicsBody.collisionBitMask = tokenmask | playermask;
powerToken.physicsBody.contactTestBitMask = playermask;
powerToken.physicsBody.usesPreciseCollisionDetection = YES;
powerToken.physicsBody.dynamic = NO;
powerToken.physicsBody.affectedByGravity = NO;
[self addChild:powerToken];
SKAction *moveDownThenDie = [SKAction sequence:@[
[SKAction waitForDuration:0.10 withRange:0.35],
[SKAction moveByX:0.0 y:-1600 duration:25],
[SKAction removeFromParent]]];
powerToken.zPosition = tokenz;
[powerToken runAction: moveDownThenDie];
temp = powerToken;
return powerToken;
}
解決
Found the answer to my problem... Dynamic was set to NO for the power up token object. This disabled spritekit from checking for collision detection on this object.
powerToken.physicsBody.dynamic = NO;