1

我正在开发一款将触摸检测与圆形 skShapenodes 相结合的游戏,并且找不到检查它们是否正在触摸的好方法。播放器类的代码如下

-(void)SpawnPlayer
{   
        _player = [[SKShapeNode alloc] init];

        CGMutablePathRef myPath = CGPathCreateMutable();
        CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
        _player.path = myPath;

        _player.lineWidth = 1.0;
        _player.fillColor = [SKColor whiteColor];
        _player.strokeColor = [SKColor whiteColor];
        _player.glowWidth = 0.5;
        _location = CGPointMake(375, 400);
        _player.position = CGPointMake(375, 400);
        _player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:0.5];
        _player.physicsBody.dynamic = NO;
        _player.physicsBody.categoryBitMask = playerCategory;
        _player.physicsBody.contactTestBitMask = enemyCategory;
        _player.physicsBody.collisionBitMask = 0;


        [self addChild:_player];
}

敌人的代码类似,只是它的位掩码和测试位掩码被交换了。

4

1 回答 1

0

可以这样想:categoryBitMask指定节点的主体类型。collisionBitMask指定它可以与哪种类型的物体发生碰撞。指定哪种类型的contactTestBitMaskcontactTestBitMasks 将与之交互。

例如,要检测具有不同 categoryBitMask 的玩家和敌人之间的接触,他们的 contactTestBitMask 应该是:

_player.physicsBody.categoryBitMask = playerCategory;
_player.physicsBody.contactTestBitMask = playerCategory | enemyCategory ;


对于敌人:

 _enemy.physicsBody.categoryBitMask = enemyCategory;
 _enemy.physicsBody.contactTestBitMask =  playerCategory | enemyCategory ;


现在您可以处理方法中的联系didBeginContact方式

于 2014-04-13T22:29:14.330 回答