2017-02-11 18 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