4

I tried to display some SKNodes and I used the width and height of screen to set their positions in order to make them compatible with different screen sizes.

SKNode* obstacle = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:obstacleSize];
obstacle.position = CGPointMake(80, self.size.height * 0.38);
obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.frame.size];

But nodes' positions still change when I run the program with a different screen size (fine in 3.5 inch but wrong in 4-inch). And I found that self.size.height is not the actual height of the screen.

Does anyone know how to solve this problem?

bneely
  • 9,083
  • 4
  • 38
  • 46
tingxuezisu
  • 41
  • 1
  • 2
  • 1
    don't present scene in viewDidLoad, move it to viewWillLayoutSubviews to get correct size. Look around to find the proper way to do this. – CodeSmile Feb 02 '14 at 10:08

2 Answers2

4

[UIScreen mainScreen].bounds.size returns a CGSize with the width and height of the screen. See the UIScreen Apple documentation.

bneely
  • 9,083
  • 4
  • 38
  • 46
0

[UIScreen mainScreen].bounds.size should return the correct CGSize vector of the width and height. However, if you are in landscape, the values will be reversed, then what'd you would expect from portrait.

MathieuF
  • 3,130
  • 5
  • 31
  • 34