我正在为我正在开发的游戏原型设置基础物理测试,但是collisionBitMask无法正常工作。我希望charSprite与floorSprite发生碰撞,而不是将其向下推入遗忘状态(由于重力),因此我为charSprite分配了Test1的categoryBitMask和Test2的碰撞BitMask,反之亦然,将floorSprite分配给charSprite。当Test1 = 0和Test2 = 0b10时,这很好用,但是当我尝试将Test1和Test2的值更改为0b20和0b30或除0和0b10之外的任何其他值时,它不起作用并且floorSprite被下推被charSprite遗忘。这是一个问题,因为我不仅仅需要2个位掩码类别,而且仅值0和0b10有效,即使那样,它也仅在charSprite的categoryBitMask为0,collisionBitMask为0b10和floorSprite为0b10,0时起作用。当charSprite的categoryBitMask为1,collisionBitMask为0,floorSprite为0,0b10时,它甚至不起作用!我找不到有效的位掩码的任何值!谁能帮我?这是我的代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct ColliderType {
static let Test1: UInt32 = 0
static let Test2: UInt32 = 0b10
}
var charSprite:SKSpriteNode!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
println("Running")
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -2)
// Add sprites
charSprite = SKSpriteNode(imageNamed: "MainCharacterTest")
charSprite.position = CGPoint(x: self.size.width/2 - 300, y: self.size.height/2 + 200)
charSprite.zPosition = 1
charSprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: charSprite.size.width, height: charSprite.size.height))
charSprite.physicsBody?.allowsRotation = false
charSprite.physicsBody?.dynamic = true
charSprite.physicsBody?.categoryBitMask = ColliderType.Test2
charSprite.physicsBody?.collisionBitMask = ColliderType.Test1
charSprite.physicsBody?.contactTestBitMask = ColliderType.Test1
self.addChild(charSprite)
let floorSprite = SKSpriteNode(imageNamed: "testthing")
floorSprite.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 200)
floorSprite.zPosition = -1
floorSprite.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: floorSprite.size.width * 1, height: floorSprite.size.height * 1))
floorSprite.physicsBody?.affectedByGravity = false
floorSprite.physicsBody?.categoryBitMask = ColliderType.Test1
floorSprite.physicsBody?.collisionBitMask = ColliderType.Test2
floorSprite.physicsBody?.contactBitMask = ColliderType.Test2
self.addChild(floorSprite)
func didBeginContact(contact: SKPhysicsContact) {
if let node1 = contact.bodyA.node {
// charSprite.removeFromParent()
// self.addChild(gameover)
}
}
func didEndContact(contact: SKPhysicsContact) {
// pass
}
}
最佳答案
尝试将地面精灵的collision bit mask
设置为0。这应该让两个精灵彼此不穿过,但是当角色落在地面上时地面不会移动
关于swift - SpriteKit的Swift CollisionBitMask值不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31258839/