节点从纵向模式消失到横向模式(Sprite Kit | Swift | Flappy Bird)

Nodes disappear from portrait to landscape mode (Sprite Kit | Swift | Flappy Bird)

我正在制作 Flappy Bird 的克隆版。我 运行 遇到了一个错误,当我从纵向模式切换到横向模式时,我的地面节点消失了。

编辑:我的代码基于此:https://github.com/fullstackio/FlappySwift

如你所见,卡比就像底部是地面一样,所以很明显节点就在那里,只是看不见。这是相关代码:

override func didMoveToView(view: SKView) {
   ...
   backgroundScrollUpdate()
   ...
}

func backgroundScrollUpdate() {
   ...
   let groundTexture = SKTexture(imageNamed: "Ground")
   groundTexture.filteringMode = SKTextureFilteringMode.Nearest
   let moveGroundDur = 0.02 * groundTexture.size().width*2
   let moveGroundSprite = SKAction.moveByX(-groundTexture.size().width*2, y:0, duration:NSTimeInterval(moveGroundDur))
   let resetGroundSprite = SKAction.moveByX(groundTexture.size().width*2, y:0, duration:0)
   let moveGroundSpriteForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite, resetGroundSprite]))
   // ground
   for var i:CGFloat = 0; i < 2 + self.frame.size.width / (groundTexture.size().width*2); ++i {
      let groundSprite = SKSpriteNode(texture: groundTexture)
      groundSprite.setScale(2.0)
      groundSprite.position = CGPointMake(i * groundSprite.size.width, groundSprite.size.height / 2)
      groundSprite.runAction(moveGroundSpriteForever)
      moving!.addChild(groundSprite)
   }
   // ground physics
   let dummyGround = SKNode()
   dummyGround.position = CGPointMake(0, groundTexture.size().height)
   dummyGround.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height*2))
   dummyGround.physicsBody?.dynamic = false
   dummyGround.physicsBody?.categoryBitMask = worldCategory
   self.addChild(dummyGround)
   ...
}

提前致谢。

当旋转为横向时,框架大小不会改变。 (例如,移动到横向后它仍然是 768x1024 而不是 1024x768)。

您需要使用 'bounds' 而不是 'frame' 来放置地面。照原样,你把它放在底部。地面就在那里,只是你看不见而已 ;)

self.view.bounds

例如:iOS UIView get frame after rotation

例如:"Incorrect" frame / window size after re-orientation in iPhone

最佳 Link : How to get orientation-dependent height and width of the screen?