2016-06-13 90 views
1

我有三个物理类别:英雄,地面和墙壁。我的问题是,地面的物理类别没有被识别。为了解决这个问题,当英雄与墙壁和地面碰撞时,我打印了这个掩码。 Wall按预期工作,显示它的位掩码为2.然而,地面显示4294967295.(应该是4.)地面有一个边缘物理体,它的工作原理是因为英雄没有穿过它,它只是不被识别为地面。SpriteKit categoryBitMask not recognized

物理学类

enum PhysicsCategory:UInt32 
{ 
    case hero = 1 
    case wall = 2 
    case ground = 4 
} 

地类:

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!") 
     } 

回答

1

你实际上是在之后创建了物理体,你试图设置categoryBitMask。所以,你要设定的categoryBitMask上零...

你只需要移动一个排队...

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 
} 
+1

我知道我应该避免在意见表示感谢,但我会为此冒险......谢谢你! – squarehippo10