SpriteKit:对象在彼此之下

时间:2017-12-18 16:31:02

标签: swift sprite-kit skspritenode

我的游戏中有两个坦克,一个是红色,一个是蓝色。

我已经设置了他们的PhysicsBody,但他们没有相互击打,而是互相攻击。

Simulator Screenshot (1)

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

1 个答案:

答案 0 :(得分:4)

只需更改行

即可
enemyTank.physicsBody?.collisionBitMask = physicsCategory.Enemy

enemyTank.physicsBody?.collisionBitMask = physicsCategory.MainTank