2017-02-23 32 views
2

我正在製作一個小型遊戲,比如飛揚的小鳥。我不知道爲什麼物體不能「看見」自己。 我正在添加一個Wall,Ground和一個Ghost,而且奇怪的是,Ghost檢測到一個Ground,反之亦然,但Wall仍然是看不見的。爲什麼?spritekit對象不能互相檢測

一個幽靈應該停在垂直矩形(牆)上,不要落在地上。 enter image description here

struct PhysicsCatagory{ 
    static let Ghost : UInt32 = 0x1 << 1 
    static let Wall : UInt32 = 0x1 << 2 
    static let Ground : UInt32 = 0x1 << 3 
} 

class GameScene: SKScene{ 

    var Ground = SKSpriteNode() 
    var Ghost = SKSpriteNode() 
    var Wall = SKSpriteNode() 



    override func didMove(to view: SKView) { 

     /*******adding ground***/ 
     Ground = SKSpriteNode(imageNamed: "Ground") 
     Ground.setScale(0.7) 
     Ground.position = CGPoint(x: self.frame.midX, y: self.frame.minY) 

     Ground.physicsBody = SKPhysicsBody(rectangleOf: Ground.size) 
     Ground.physicsBody?.categoryBitMask = PhysicsCatagory.Ground 
     Ground.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost 
     Ground.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost 
     Ground.physicsBody?.affectedByGravity = false 
     Ground.physicsBody?.isDynamic = false 
     Ground.zPosition = 3 
     self.addChild(Ground) 

     /*******adding wall (not working)****/ 
     Wall = SKSpriteNode(imageNamed: "Wall") 
     Wall.setScale(0.7) 
     Wall.position = CGPoint(x: self.frame.midX, y: self.frame.midY + 100) 

     Wall.physicsBody? = SKPhysicsBody(rectangleOf: Wall.size) 
     Wall.physicsBody?.categoryBitMask = PhysicsCatagory.Wall 
     Wall.physicsBody?.collisionBitMask = PhysicsCatagory.Ghost 
     Wall.physicsBody?.contactTestBitMask = PhysicsCatagory.Ghost 
     Wall.physicsBody?.affectedByGravity = false 
     Wall.physicsBody?.isDynamic = false 
     Wall.zPosition = 1 
     self.addChild(Wall) 


     /*******adding ghost***/ 
     Ghost = SKSpriteNode(imageNamed: "Ghost") 
     Ghost.size = CGSize(width: 90, height: 100) 
     Ghost.position = CGPoint(x: self.frame.midX, y: self.frame.maxY) 

     Ghost.physicsBody = SKPhysicsBody(circleOfRadius: 50) 
     Ghost.physicsBody?.categoryBitMask = PhysicsCatagory.Ghost 
     Ghost.physicsBody?.collisionBitMask = PhysicsCatagory.Wall | PhysicsCatagory.Ground 
     Ghost.physicsBody?.contactTestBitMask = PhysicsCatagory.Wall | PhysicsCatagory.Ground 
     Ghost.physicsBody?.affectedByGravity = true 
     Ghost.physicsBody?.isDynamic = true 
     Ghost.zPosition = 2 
     self.addChild(Ghost) 

    } 
+0

您可以在您的GameViewController中將showPhysics設置爲true,以便顯示物理實體? –

+0

did not''help :(但實際上物理顯示,但只適用於地面和鬼魂,不適用於牆壁 – belab

+0

然後我猜測牆壁沒有物理體,出於一些奇怪的原因。嘗試把打印(「 Wall的PB是\(wall.physicsbody)「),在你設置了牆的PB之後,確認這個。 –

回答

4

在此請看:

Wall.physicsBody? = SKPhysicsBody(rectangleOf: Wall.size) 

注意這裏的問號。它用於指定optional chaining

可選鏈是一個查詢和調用可能當前爲零的可選屬性,方法和下標的過程。如果可選包含一個值,則屬性,方法或下標調用會成功;如果可選爲零,則property,method或subscript調用返回nil。

現在,作業也是可選鏈接的一部分。所以如果一個屬性是零,分配將失敗。

在你的情況下,一個physicsBody屬性是零,所以=運算符右邊的代碼都不會被評估。

爲了使這項工作,做到這一點:

Wall.physicsBody = SKPhysicsBody(rectangleOf: Wall.size) 

事情無關的這一點,但好東西要記住:

如果你是兩個機構之間的興趣接觸檢測,至少有一個必須是動態的。我指出了這一點,因爲您的牆和地面物體是靜態的,這意味着無論您如何設置聯繫人蒙版,都不會在它們之間檢測到任何聯繫人。

哦,是的,使用camelCase符號來命名類,屬性等等。

+0

好點 - 我錯過了額外的'?'。但是我對PB不存在:-) –

+0

@Whirlwind非常感謝你!大概我已經複製了一些代碼,並沒有注意到這個問號。 – belab

+0

@Steve Ives喲,謝謝你! – belab