没有检测到碰撞

时间:2014-04-03 19:58:38

标签: xcode5 sprite-kit

我有两个精灵,我的玩家和碰撞的墙(来自瓷砖地图)。下面我已经粘贴了播放器和墙壁的代码

Player.m
    - (instancetype)init {
        SKTextureAtlas *atlas =
        [SKTextureAtlas atlasNamed: @"char"];
        SKTexture *texture = [atlas textureNamed:@"player"]; texture.filteringMode = SKTextureFilteringNearest;
        if (self = [super initWithTexture:texture])
        { self.name = @"player";
            CGFloat minDiam = MIN(self.size.width, self.size.height);
            minDiam = MAX(minDiam-16, 4);
            self.physicsBody =
            [SKPhysicsBody bodyWithCircleOfRadius:minDiam/2.0];
            self.physicsBody.categoryBitMask=PCPlayerCategory;    
            self.physicsBody.collisionBitMask =
            PCWallCategory;
            self.physicsBody.allowsRotation = NO;
            self.physicsBody.restitution = 0;
            self.physicsBody.friction = 0;
            self.physicsBody.linearDamping = 0;
        }
        return self;
    }

tilemap.m

        SKSpriteNode *tile = [layer tileAtCoord:coord];
        tile.name=@"wall";
        tile.physicsBody =
        [SKPhysicsBody bodyWithRectangleOfSize:tile.size];
        tile.physicsBody.categoryBitMask = PCWallCategory;
          tile.physicsBody.collisionBitMask =
          PCPlayerCategory;
        tile.physicsBody.dynamic = NO;
        tile.physicsBody.friction = 0;
          tile.physicsBody.restitution=0;

在我的mainscene.m

- (void)didBeginContact:(SKPhysicsContact *)contact {
  uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
  if (collision == (PCWallCategory|PCPlayerCategory)) {
    NSLog(@"SUCCESS"); }

然而,即使我的播放器坐在我的墙上,我也没有看到NSLog出现,希望能得到一些帮助和洞察力。 谢谢

- (void)didBeginContact:(SKPhysicsContact *)contact {
  uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
  if (collision == (PCWallCategory|PCPlayerCategory)) {

    NSLog(@"SUCCESS");
    onGround=YES;
  }else
  {
    onGround=NO;
  }

}

1 个答案:

答案 0 :(得分:0)

collisionTestBitMask仅定义哪些类型的物体可以与physicsBody碰撞。为了在两个实体发生碰撞时触发联系代表,您需要设置contactTestBitMask

所以,physicsBody应该用这种方式定义:

self.physicsBody.categoryBitMask=PCPlayerCategory;    
self.physicsBody.contactTestBitMask = PCWallCategory;