このプロジェクトには 2 人のプレイヤーがいて、どちらも射撃できます。反対側のプレイヤーの弾丸が船に当たると、体力が低下して消えますが、これはうまくいきます。問題は、船や弾丸が互いに衝突することに反応してほしくないということです。
最初のプレーヤーを作成するためのコードは次のとおりです。
player = [SKSpriteNode spriteNodeWithImageNamed:filePath];
player.size = CGSizeMake(100, 100);
player.position = CGPointMake(150, 250);
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.physicsBody.categoryBitMask = playerCategory;
player.physicsBody.collisionBitMask = wallCategory;
player.physicsBody.friction = 1.0f;
player.physicsBody.restitution = 1.0f;
player.physicsBody.linearDamping = 1.0f;
player.physicsBody.allowsRotation = NO;
[self addChild:player];
そして 2 番目のプレーヤー:
playerTwo = [SKSpriteNode spriteNodeWithImageNamed:filePath];
playerTwo.size = CGSizeMake(100, 100);
playerTwo.position = CGPointMake((384*2)-150, (512*2)-250);
playerTwo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:playerTwo.size];
playerTwo.physicsBody.categoryBitMask = playerTwoCategory;
playerTwo.physicsBody.collisionBitMask = wallCategory;
playerTwo.physicsBody.friction = 1.0f;
playerTwo.physicsBody.restitution = 1.0f;
playerTwo.physicsBody.linearDamping = 1.0f;
playerTwo.physicsBody.allowsRotation = NO;
[self addChild:playerTwo];
カテゴリの初期化:
static const uint32_t playerCategory = 2;
static const uint32_t playerTwoCategory = 5;
static const uint32_t wallCategory = 1;
static const uint32_t bulletCategory = 4;
static const uint32_t bulletTwoCategory = 7;
プレーヤー 1 の弾丸作成:
SKSpriteNode * bullet = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5, 5)];
bullet.position = [gunOne convertPoint:CGPointMake(0,0) toNode:self];
[self addChild:bullet];
bullet.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bullet.frame.size];
bullet.physicsBody.categoryBitMask = bulletCategory;
bullet.physicsBody.dynamic = YES;
bullet.physicsBody.contactTestBitMask = playerTwoCategory | wallCategory;
bullet.physicsBody.collisionBitMask = wallCategory;
bullet.physicsBody.usesPreciseCollisionDetection = YES;
プレーヤー 2 の弾丸作成:
SKSpriteNode * bulletTwo = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(5, 5)];
bulletTwo.position = [gunOneTwo convertPoint:CGPointMake(0,0) toNode:self];
[self addChild:bulletTwo];
bulletTwo.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bulletTwo.frame.size];
bulletTwo.physicsBody.categoryBitMask = bulletTwoCategory;
bulletTwo.physicsBody.dynamic = YES;
bulletTwo.physicsBody.contactTestBitMask = playerCategory | wallCategory;
bulletTwo.physicsBody.collisionBitMask = wallCategory;
bulletTwo.physicsBody.usesPreciseCollisionDetection = YES;
これが行われる方法に、最初のプレイヤーと弾丸が衝突に反応するだけの目に見える問題はありますか? 助けてくれてありがとう、そして長い間読んでごめんなさい。