SpriteKit:对象彼此下方

Posted

技术标签:

【中文标题】SpriteKit:对象彼此下方【英文标题】:SpriteKit: Objects go underneath each other 【发布时间】:2017-12-18 16:31:02 【问题描述】:

我的游戏中有两辆坦克,一辆红色和一辆蓝色。

我已经设置了它们的 PhysicsBody,但它们并没有相互碰撞,而是在彼此下方。

Simulator Screenshot (1)

物理类别:

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的collisionBitMask设置为MainTank的categorybitMask @Muffinman2497,那你能纠正我吗? 【参考方案1】:

换行就行了

enemyTank.physicsBody?.collisionBitMask = physicsCategory.Enemy

enemyTank.physicsBody?.collisionBitMask = physicsCategory.MainTank

【讨论】:

以上是关于SpriteKit:对象彼此下方的主要内容,如果未能解决你的问题,请参考以下文章

CAShapeLayer 和 SpriteKit

Spritekit 碰撞检测无法正常工作

SpriteKit - SKCameraNode 视差效果?

SpriteKit:SKSpriteNodes 之间的差距

SpriteKit 节点旋转问题

SpriteKit 对同一类型的多个对象的碰撞检测