2017-02-11 32 views
0

我目前正在製作一個遊戲,其中有一個主要的playerSprite,每當我點擊屏幕時跳轉。每隔一會兒,一個fattySprite節點就從天而降,堆積在playerSprite之上。每增加一個節點堆疊,跳躍高度就會降低。爲什麼跳高會降低?我嘗試在fattySprite節點中將各種physicsBody屬性設置爲0,試圖防止這些額外的節點影響跳躍高度。節點與physicsBody.mass = 0堆疊在主節點正在影響主節點的跳躍高度

代碼片段:

override func didMove(to view: SKView) { 
     self.physicsWorld.contactDelegate = self 
     self.anchorPoint = CGPoint(x: 0, y: 0) 

     // Character creation 
     self.playerSprite = PlayerNode(position: CGPoint(x: self.frame.size.width/9, y: self.frame.size.height/4), 
             size: CGSize(width: self.frame.size.width/5, height: self.frame.size.height/10)) 
     self.playerSprite?.physicsBody?.categoryBitMask = playerCategory 
     self.playerSprite?.physicsBody?.contactTestBitMask = playerCategory | trapCategory 
     self.playerSprite?.physicsBody?.affectedByGravity = true 
     self.playerSprite?.physicsBody?.restitution = 0.0 
     self.playerSprite?.physicsBody?.friction = 0 
     self.addChild(self.playerSprite!) 
     self.playerSprite?.beginAnimation() 

     // Fatties spawning 
     let wait_fatty = SKAction.wait(forDuration: 3, withRange: 2) 
     let spawnFatty = SKAction.run({ 
      let fattySprite = PlayerNode(position: CGPoint(x: self.frame.size.width/9, y: self.frame.size.height), 
             size: CGSize(width: self.frame.size.width/5, height: self.frame.size.height/10)) 
      fattySprite.physicsBody?.categoryBitMask = self.fattiesCategory 
      fattySprite.physicsBody?.contactTestBitMask = self.fattiesCategory | self.playerCategory 
      fattySprite.physicsBody?.mass = 0 
      fattySprite.physicsBody?.allowsRotation = false 
      fattySprite.physicsBody?.linearDamping = 0 
      fattySprite.physicsBody?.angularDamping = 0 
      fattySprite.physicsBody?.restitution = 0.0 
      fattySprite.physicsBody?.friction = 0 
      self.addChild(fattySprite) 

     }) 
     let fattySequence = SKAction.sequence([wait_fatty, spawnFatty]) 
     self.run(SKAction.repeatForever(fattySequence)) 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if self.playerSprite?.action(forKey: "jump") == nil { // check that there's no jump action running 
      //self.playerSprite?.physicsBody?.velocity = CGVector(dx: 0, dy: 500) 
      self.playerSprite?.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 500)) 
     } 
    } 

回答

0

您必須創建到它給值之前physicsbody。 在對fattySprite.physicsbody的所有調用中?你正試圖給一個零變量賦值。

這樣做: fattySprite.physicsbody = SKPhysicsbody(rectangleOf:fattySprite.size)

,然後分配所有的物理性質。我看不到你是如何構建你的playernode的,可能會有更合適的physicsbody-init函數。檢查文檔:) https://developer.apple.com/reference/spritekit/skphysicsbody