SpriteKit categoryBitMask 无法识别
SpriteKit categoryBitMask not recognized
我有三个物理类别:英雄、地面和墙。我的问题是地面的物理类别没有被识别。为了解决这个问题,我在 hero 与墙壁和地面碰撞时打印了位掩码。 Wall 按预期工作并显示其位掩码 2。然而,Ground 显示 4294967295。(它应该是 4。)Ground 有一个边缘物理体并且它起作用是因为英雄没有穿过它,它只是没有被识别为地面。
物理类别
enum PhysicsCategory:UInt32
{
case hero = 1
case wall = 2
case ground = 4
}
地面class:
class Ground: SKSpriteNode
{
var groundTexture = SKTexture(imageNamed: "ground4")
var jumpWidth = CGFloat()
var jumpCount = CGFloat(1)
func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
{
parentNode.addChild(self)
self.size = size
self.position = position
self.zPosition = 2
self.anchorPoint = CGPointMake(0, 1)
self.texture = SKTexture(imageNamed: "ground4")
self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = false
let pointTopRight = CGPoint(x: size.width, y: 0)
self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
}
didMoveToView:
let groundPosition = CGPoint(x: -self.size.width, y: 30)
let groundSize = CGSize(width: self.size.width * 3, height: 0)
ground.spawn(world, position: groundPosition, size: groundSize)
didBeginContact
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == PhysicsCategory.hero.rawValue && secondBody.categoryBitMask == PhysicsCategory.ground.rawValue || firstBody.categoryBitMask == PhysicsCategory.ground.rawValue && secondBody.categoryBitMask == PhysicsCategory.hero.rawValue
{
print("contact with the ground!")
}
您实际上是在尝试设置 categoryBitMask 之后创建 physicsBody。所以你试图将 categoryBitMask 设置为 nil...
你只需要将那一行移动...
func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
{
parentNode.addChild(self)
self.size = size
self.position = position
self.zPosition = 2
self.anchorPoint = CGPointMake(0, 1)
self.texture = SKTexture(imageNamed: "ground4")
let pointTopRight = CGPoint(x: size.width, y: 0)
self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = false
}
我有三个物理类别:英雄、地面和墙。我的问题是地面的物理类别没有被识别。为了解决这个问题,我在 hero 与墙壁和地面碰撞时打印了位掩码。 Wall 按预期工作并显示其位掩码 2。然而,Ground 显示 4294967295。(它应该是 4。)Ground 有一个边缘物理体并且它起作用是因为英雄没有穿过它,它只是没有被识别为地面。
物理类别
enum PhysicsCategory:UInt32
{
case hero = 1
case wall = 2
case ground = 4
}
地面class:
class Ground: SKSpriteNode
{
var groundTexture = SKTexture(imageNamed: "ground4")
var jumpWidth = CGFloat()
var jumpCount = CGFloat(1)
func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
{
parentNode.addChild(self)
self.size = size
self.position = position
self.zPosition = 2
self.anchorPoint = CGPointMake(0, 1)
self.texture = SKTexture(imageNamed: "ground4")
self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = false
let pointTopRight = CGPoint(x: size.width, y: 0)
self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
}
didMoveToView:
let groundPosition = CGPoint(x: -self.size.width, y: 30)
let groundSize = CGSize(width: self.size.width * 3, height: 0)
ground.spawn(world, position: groundPosition, size: groundSize)
didBeginContact
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == PhysicsCategory.hero.rawValue && secondBody.categoryBitMask == PhysicsCategory.ground.rawValue || firstBody.categoryBitMask == PhysicsCategory.ground.rawValue && secondBody.categoryBitMask == PhysicsCategory.hero.rawValue
{
print("contact with the ground!")
}
您实际上是在尝试设置 categoryBitMask 之后创建 physicsBody。所以你试图将 categoryBitMask 设置为 nil...
你只需要将那一行移动...
func spawn(parentNode: SKNode, position: CGPoint, size:CGSize)
{
parentNode.addChild(self)
self.size = size
self.position = position
self.zPosition = 2
self.anchorPoint = CGPointMake(0, 1)
self.texture = SKTexture(imageNamed: "ground4")
let pointTopRight = CGPoint(x: size.width, y: 0)
self.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointZero, toPoint: pointTopRight)
self.physicsBody?.categoryBitMask = PhysicsCategory.ground.rawValue
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = false
}