2016-06-13 88 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