0

I am working on a Flappy Bird clone. I'm running into a bug where my ground nodes disappear when I switch from portrait mode to landscape mode.

EDIT: I am basing my code off this: https://github.com/fullstackio/FlappySwift

Portrait mode

Landscape mode

As you can see, Kirby acts like the bottom is the ground, so it's clear that the node is there, it just cannot be seen. Here's the relevant code:

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

Thanks in advance.

Mike
  • 1,307
  • 3
  • 17
  • 29

1 Answers1

3

When rotated to landscape the frame size doesn't change. (e.g. it is still 768x1024 instead of 1024x768 after moving to landscape).

You'll want to use 'bounds' instead of 'frame' for placing your ground. As is, you placed it off the bottom. The ground is there, you just can't see it ;)

self.view.bounds

eg : iOS UIView get frame after rotation

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

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

Community
  • 1
  • 1
LawfulEvil
  • 2,267
  • 24
  • 46
  • Thanks for your answer, I switched all my self.frame.size.* to self.view!.bounds.* and it loads the textures a lot slower now and Kirby will fall through the ground b/c it its not loaded. Also in landscape it's still not showing. Am I missing something? – Mike Apr 07 '15 at 19:09
  • Fixed the bug w/ Kirby falling through by using self.view!.bounds.height instead of .width. They're switched for view.bounds. However, in landscape mode, the ground is still not visible. – Mike Apr 07 '15 at 19:15
  • You didn't post the code that deals with the rotation and moves all the components to the correct place. I would look at the code setting the position of kirby (middle left to right and bottom edge) and compare to the code which draws the ground. It might be attached to the wrong view too. – LawfulEvil Apr 07 '15 at 19:25
  • I don't have any extra code that deals w/ rotation of the nodes when the orientation is changed...is that a source of my bug? – Mike Apr 07 '15 at 19:28
  • Probably, also note that all of this is IOS dependent. On IOS 7.2 and less it works diff. http://stackoverflow.com/questions/7905432/how-to-get-orientation-dependent-height-and-width-of-the-screen/7905540#7905540 So, sometimes width=bounds.height and other times width=bounds.width. – LawfulEvil Apr 07 '15 at 19:32