我正在製作一個小型遊戲,比如飛揚的小鳥。我不知道爲什麼物體不能「看見」自己。 我正在添加一個Wall,Ground和一個Ghost,而且奇怪的是,Ghost檢測到一個Ground,反之亦然,但Wall仍然是看不見的。爲什麼?spritekit對象不能互相檢測
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)
}
您可以在您的GameViewController中將showPhysics設置爲true,以便顯示物理實體? –
did not''help :(但實際上物理顯示,但只適用於地面和鬼魂,不適用於牆壁 – belab
然後我猜測牆壁沒有物理體,出於一些奇怪的原因。嘗試把打印(「 Wall的PB是\(wall.physicsbody)「),在你設置了牆的PB之後,確認這個。 –