iPhone 5s SpriteKit drawing oddities

168 views Asked by At

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

1

There are 1 answers

5
Whirlwind On BEST ANSWER

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.