我的游戏中有两个坦克,一个是红色,一个是蓝色。
我已经设置了他们的PhysicsBody,但他们没有相互击打,而是互相攻击。
PhysicsCategory:
struct physicsCategory {
static let Bullet : UInt32 = 0x1 << 0
static let Enemy : UInt32 = 0x1 << 1
static let MainTank : UInt32 = 0x1 << 2
static let EnemyTank : UInt32 = 0x1 << 3
}
这是蓝色坦克代码:
MainTank = (self.childNode(withName: "tank") as? SKSpriteNode)!
MainTank.name = "blueTank"
MainTank.physicsBody?.categoryBitMask = physicsCategory.MainTank
MainTank.physicsBody?.collisionBitMask = physicsCategory.Enemy
MainTank.physicsBody?.contactTestBitMask = physicsCategory.Enemy
红色坦克:
enemyTank = SKSpriteNode(imageNamed: "tank_red")
enemyTank.size = CGSize(width: 76, height: 92)
enemyTank.name = "enemyTank"
enemyTank.zPosition = 0
enemyTank.physicsBody = SKPhysicsBody(rectangleOf: enemyTank.size)
enemyTank.physicsBody?.categoryBitMask = physicsCategory.EnemyTank
enemyTank.physicsBody?.collisionBitMask = physicsCategory.Enemy
enemyTank.physicsBody?.contactTestBitMask = physicsCategory.MainTank
enemyTank.physicsBody?.isDynamic = false
答案 0 :(得分:4)
只需更改行
即可enemyTank.physicsBody?.collisionBitMask = physicsCategory.Enemy
要
enemyTank.physicsBody?.collisionBitMask = physicsCategory.MainTank