1

I understand that the iPhone 5s has a pixel resolution of 640 x 1136 and a point resolution of 320 x 568 (for backward compatibility with non-Retina devices).

The problem/confusion/oddity seems to come in when I'm working with SpriteKit. Example:

I'm drawing a line from bottom-left corner (0, 0) to top-right corner (width, height). The result was that the line was drawn almost halfway. And indeed when i print out the screen size, it should 320 x 568. So i decided to draw from (0, 0) to (width * 2, height * 2). And of course, this printed out 640 x 1136.

So the oddity is this: Even though I'm drawing from what should be a diagonal line corner to corner, it is not, in actuality, being drawn from corner to corner.

Notes:

 - I'm getting the width & height values from self.value.frame.size.
 - The diagonal line seems to draw just fine using any of the iPad simulators.

Any ideas what is going on here? enter image description here

AlvinfromDiaspar
  • 6,611
  • 13
  • 75
  • 140
  • I can't produce what you are saying..If I draw a line between (0,0) and (width, height) points I get a line from bottom left corner to the upper left corner. What you can check is scene scale mode in your controller as well as to move the code which draws line, from scene's init method into didMoveToView method. If you need a code, I will write an answer. Also code example on how you doing everything (and in which methods) would be helpful in debugging. – Whirlwind Jun 08 '15 at 16:27

1 Answers1

1

Anyways here is how I got good results:

Just open up a new project and try this:

In your GameViewContrller instead of using viewDidLoad use viewWillLayoutSubviews.

EDIT: Here is a good explanation by Rob Mayoff about methods like viewDidLoad and viewWillLayoutSubviews.

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = YES;

    // Create and configure the scene.

    if(!skView.scene){
        GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
        scene.scaleMode = SKSceneScaleModeAspectFill;

        // Present the scene.
        [skView presentScene:scene];
    }
}

So now in your didMoveToView method in scene class, just draw a line :

    SKShapeNode *yourline = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, 0.0, 0.0);
    CGPathAddLineToPoint(pathToDraw, NULL, self.frame.size.width,self.frame.size.height);
    yourline.path = pathToDraw;
    [yourline setStrokeColor:[UIColor redColor]];
    [self addChild:yourline];
    CGPathRelease(pathToDraw);

Read this about init vs didMoveToView (read comments posted by LearnCocos2D).

So this is pretty much it, and I hope it helps.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • This could do the trick. I will try this when i get home. But for now, i still dont see why things are working for all iPad simulators. My scaling is AspectFit. And i do believe i am drawing things from the didMoveToView method (but i need to verify this). – AlvinfromDiaspar Jun 08 '15 at 17:31
  • @AlvinfromDiaspar You should check how you initialize the scene in view controller. In viewDidLoad the final size of the view might not be known yet . I will update my answer with a good link which describes more in depth differences between viewDidLoad and viewWillLayoutSubviews. Also note that in my example I don't create scene from .sks file. – Whirlwind Jun 08 '15 at 17:54
  • Is it preferable to NOT loading scene from .sks file? – AlvinfromDiaspar Jun 08 '15 at 22:41
  • @AlvinfromDispar Nothing wrong with creating scene from .sks. Just keep in mind that when creating a scene from .sks the scene size is determined by GameScene.sks file (I think that default size is 1024x768). But you can change that like described here: http://stackoverflow.com/a/26266857/3402095 – Whirlwind Jun 08 '15 at 22:50
  • I am creating on viewWillLayoutSubviews, and drawing the scene on didMoveToView. I think the problem is that my first scene is loaded by .sks (which is sized for iPad). And the 2nd scene i create programmatically using the first scene's size. – AlvinfromDiaspar Jun 09 '15 at 04:39