【发布时间】:2017-07-13 20:50:36
【问题描述】:
我正在做一个小游戏,比如小鸟。我不知道为什么物体不能“看到”我们自己。 我添加了墙、地面和幽灵,很奇怪,幽灵检测到地面,反之亦然,但墙仍然不可见。为什么?
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,以便显示物理实体吗?
-
没有帮助:(但实际上显示了物理,但仅适用于地面和幽灵,不适用于墙壁。
-
那么我猜这堵墙没有物理体,出于某种奇怪的原因。尝试在设置 wall 的 PB 后在某处放置 print("Wall's PB is (wall.physicsbody)") 以确认这一点。
-
试试这个例子中的 checkPhysics() 函数:stackoverflow.com/documentation/sprite-kit/6261/…。将函数复制到您的代码中,并在设置完所有内容后通过 checkPhysics() 调用它。
-
什么不起作用?
标签: swift sprite-kit collision-detection collision