0

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

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

人像模式

横向模式

正如你所看到的,Kirby 的行为就像底部是地面,所以很明显节点在那里,只是看不到。以下是相关代码:

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)
   ...
}

提前致谢。

4

1 回答 1

3

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

您需要使用“边界”而不是“框架”来放置地面。照原样,你把它放在底部。地面在那里,你只是看不到它;)

self.view.bounds

例如:iOS UIView 旋转后获取帧

例如:在 iPhone 中重新定向后“不正确”的框架/窗口大小

最佳链接: 如何获得与方向相关的屏幕高度和宽度?

于 2015-04-07T18:58:42.880 回答