如何正确修复SpriteKit中的联系人?

时间:2014-07-26 17:37:35

标签: xcode ios7 sprite-kit

我使用此代码并且它必须工作,但它不会:

static const uint32_t platformCategory  =  0x1 << 1;
static const uint32_t playerCategory  =  0x1 << 2;

播放器代码:

self.player = [SKSpriteNode spriteNodeWithImageNamed:@"player"];
self.player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.player.size];
self.player.physicsBody.dynamic = false;
self.player.physicsBody.friction = 0;
self.player.physicsBody.usesPreciseCollisionDetection = true;
self.player.physicsBody.categoryBitMask = playerCategory;
self.player.physicsBody.collisionBitMask = platformCategory;
self.player.physicsBody.contactTestBitMask = platformCategory;
[self addChild:self.player];

“平台”的代码:

- (void)addPlatformRight {

  int plType = arc4random()%3;

  SKSpriteNode * platform = [SKSpriteNode spriteNodeWithImageNamed:[NSString      stringWithFormat:@"platform%d", plType]];
  platform.position = CGPointMake(self.frame.size.width+100, self.frame.size.height-200);
  platform.size = CGSizeMake(platform.size.width*1.2, platform.size.height*2);
  platform.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:platform.size];
  platform.physicsBody.dynamic = false;
  platform.physicsBody.friction = 0;
  platform.physicsBody.usesPreciseCollisionDetection = true;
  platform.physicsBody.categoryBitMask = platformCategory;
  platform.physicsBody.contactTestBitMask = playerCategory;
  platform.physicsBody.collisionBitMask = playerCategory;

  [self addChild:platform];
  [platform runAction:[SKAction sequence:@[[SKAction rotateByAngle:M_PI/4.0*8 duration:3], [SKAction removeFromParent]]]];
}

并且

- (void)didBeginContact:(SKPhysicsContact *)contact {
  NSLog(@"contact");
}

不幸的是,这不起作用。有什么错误吗?

1 个答案:

答案 0 :(得分:0)

两个重叠的非动态实体不会生成联系事件,其中至少有一个必须是动态的