2017-05-05 52 views
2

這是我的球員斯威夫特3 Physicsbody碰撞不起作用

我的球員不與Gamefield他們只是經過互相碰撞類。 我不希望他們在彼此之上。 我試圖谷歌解決方案,但對我來說,似乎我做的大多都是正確的。

請幫助:

class Player: SKShapeNode { 
    static public let length: CGFloat = 50 
    static public let rect = CGRect(x: -length/2, y: -length/2, width: length, height: length) 

    var life = 100 

    override init() { 
     super.init() 
     self.fillColor = SKColor.blue 
     self.strokeColor = SKColor.black 
     self.position = CGPoint(x: 50, y: 50) 

     self.physicsBody = SKPhysicsBody(edgeLoopFrom: Player.rect) 
     self.physicsBody?.isDynamic = true 
     self.physicsBody?.allowsRotation = false 
     self.physicsBody?.categoryBitMask = PhysicsCategory.Robot 
     self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile 
     self.physicsBody?.collisionBitMask = PhysicsCategory.Gamefield 
     self.physicsBody?.usesPreciseCollisionDetection = true 
    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")} 

    func moveBy(vect: CGVector) { 
     self.position.x += vect.dx 
     self.position.y += vect.dy 
     //print(vect.dx, vect.dy) 
    } 
} 

這是我的GameScene類

struct PhysicsCategory { 
    static let None   : UInt32 = UInt32.min 
    static let All   : UInt32 = UInt32.max 
    static let Robot  : UInt32 = 0b0001  // 1 
    static let Gamefield : UInt32 = 0b0010  // 2 
    static let Monster  : UInt32 = 0b0011  // 3 
    static let Projectile : UInt32 = 0b0100  // 4 
} 

class GameScene: SKScene, SKPhysicsContactDelegate { 
    var player = Player(rect: Player.rect) 
    var controlL: Control 
    var controlR: Control 

    var cam = SKCameraNode() 

    override init(size: CGSize) { 
     controlL = Control(posX: 0, size: size, direction: 1) 
     controlR = Control(posX: size.width, size: size, direction: -1) 

     super.init(size: size) 

    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")} 

    override func didMove(to view: SKView) { 
     self.backgroundColor = SKColor.white 

     physicsWorld.gravity = CGVector.zero 
     physicsWorld.contactDelegate = self 

     cam.xScale = 1 
     cam.yScale = 1 
     self.camera = cam 
     player.addChild(cam) 

     let gameFieldRect = CGRect(x: 0, y: 0, width: 1000, height: 600) 
     let gameField = SKShapeNode(rect: gameFieldRect) 
      gameField.fillColor = SKColor.clear 
      gameField.strokeColor = SKColor.black 
      gameField.position = CGPoint(x: 0, y: 0) 

      gameField.physicsBody = SKPhysicsBody(edgeLoopFrom: gameFieldRect) 
      gameField.physicsBody?.isDynamic = true 
      gameField.physicsBody?.allowsRotation = false 
      gameField.physicsBody?.categoryBitMask = PhysicsCategory.Gamefield 
      gameField.physicsBody?.contactTestBitMask = PhysicsCategory.None 
      gameField.physicsBody?.collisionBitMask = PhysicsCategory.Robot 
     self.addChild(gameField) 

     self.addChild(player) 

     cam.addChild(controlL.add()) 
     cam.addChild(controlR.add()) 

我希望有人看到的錯誤。這已經讓我有了很長的準備。

回答

1

你需要你的機器人更改爲不同的物理體型(rectangleOf)?:

SpriteKit支持兩種物理機構,基於數量的機構和基於邊緣的機構。當你創建一個物理體時,它的種類,大小和形狀是由你調用的構造函數方法決定的。基於邊緣的物體不具有質量或體積,並且不受系統中的力或衝擊影響。基於邊緣的物體用於表示物理模擬中的無體積邊界或空心空間。相反,基於體積的物體用於表示具有質量和體積的物體。

此外,你是否使用衝動或力量來移動你的機器人?不是.move(到:)或.position =?移動和位置將打破你的物理世界(它會經過它在某些情況下)

此外,它看起來像你需要改變你的類口罩,以適當的系列(didBegin(接觸:))

它應該是,1,2,4,8,16,等等...這就是你如何得到獨特的接觸命中..現在你可以從0 + 4或1 + 3中得到命中「4」 ...因此,不是唯一的碰撞。

雖然你的collisionBitMask看起來不錯,但它們應該互相碰撞。