【问题标题】:Nodes disappear from portrait to landscape mode (Sprite Kit | Swift | Flappy Bird)节点从纵向模式消失到横向模式(Sprite Kit | Swift | Flappy Bird)
【发布时间】:2015-06-12 09:14:55
【问题描述】:

我正在开发 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)
   ...
}

提前致谢。

【问题讨论】:

    标签: ios swift sprite-kit flappy-bird-clone


    【解决方案1】:

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

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

    self.view.bounds
    

    例如:iOS UIView get frame after rotation

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

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

    【讨论】:

    • 感谢您的回答,我将所有 self.frame.size.* 都切换为 self.view!.bounds.* 并且它现在加载纹理的速度要慢得多,并且 Kirby 会掉到地上 b /c 它没有加载。同样在风景中它仍然没有显示。我错过了什么吗?
    • 通过使用 self.view!.bounds.height 而不是 .width 修复了 Kirby 掉线的问题。它们被切换为 view.bounds。但是,在横向模式下,地面仍然是不可见的。
    • 您没有发布处理旋转并将所有组件移动到正确位置的代码。我会查看设置 kirby 位置的代码(从左到右和底部边缘)并与绘制地面的代码进行比较。它也可能附加到错误的视图。
    • 我没有任何额外的代码来处理方向改变时节点的旋转...这是我的错误的来源吗?
    • 可能,还要注意所有这些都依赖于 IOS。在 IOS 7.2 及更低版本上,它的工作方式不同。 stackoverflow.com/questions/7905432/… 所以,有时 width=bounds.height 有时 width=bounds.width。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多