SpriteKit:对象在彼此之下
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpriteKit:对象在彼此之下相关的知识,希望对你有一定的参考价值。
我的游戏中有两个坦克,一个红色,一个蓝色。
我已经设置了他们的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
答案
只需改变线条
enemyTank.physicsBody?.collisionBitMask = physicsCategory.Enemy
至
enemyTank.physicsBody?.collisionBitMask = physicsCategory.MainTank
以上是关于SpriteKit:对象在彼此之下的主要内容,如果未能解决你的问题,请参考以下文章