两个精灵之间的碰撞

时间:2015-04-29 18:48:05

标签: objective-c sprite-kit

我试图在两个精灵之间发生碰撞,但它不起作用,但是没有检测到接触。

  

在GameScene.h中

@interface GameScene : SKScene <SKPhysicsContactDelegate>{

*

  

在GameScene.m中

static const uint32_t ballCategory = 1<<0;
static const uint32_t obs1Category = 1<<1;

*

ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball_cross"];
ball.position = CGPointMake(bx,by);
ball.name = @"ball";
ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ball.size];
ball.physicsBody.dynamic = YES;
ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.collisionBitMask = obs1Category;
ball.physicsBody.contactTestBitMask = obs1Category;

*

obs1 = [SKSpriteNode spriteNodeWithImageNamed:@"obs1"];
obs1.position = obs1XY;
obs1.name = @"obs1";
obs1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obs1.size];
obs1.physicsBody.categoryBitMask = obs1Category;
obs1.physicsBody.contactTestBitMask = ballCategory;
obs1.physicsBody.collisionBitMask = ballCategory;
obs1.physicsBody.dynamic = YES;

*

-(void)didBeginContact:(SKPhysicsContact *)contact{

   NSLog(@"hit");
}

1 个答案:

答案 0 :(得分:1)

在碰撞位掩码和接触测试位掩码中,您需要添加要从中接收通知的两个实体。

IE:

obs1.physicsBody.categoryBitMask = obs1Category;
obs1.physicsBody.contactTestBitMask = ballCategory;
obs1.physicsBody.collisionBitMask = ballCategory || obs1Category;

ball.physicsBody.categoryBitMask = ballCategory;
ball.physicsBody.collisionBitMask = obs1Category || ballCategory;
ball.physicsBody.contactTestBitMask = obs1Category;

当接触位掩码与球和球体之间的组合匹配时,将生成通知。

我希望这有助于交配!