I have a SpriteKit game in which I want to support all orientations. Right now, when I change orientation, node is not retaining its position. I use SKSceneScaleModeResizeFillto scale because it will keep the correct size of the sprite.
When I launch the game, the game player is positioned in the middle of the screen as follows:

Then, when I rotate the device, the position will look like this:

Here is my view controller code:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
SKView * skView = (SKView *)self.view;
if (!skView.scene) {
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeResizeFill;
[skView presentScene:scene];
}
}
And my scene code:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
spaceship.position = CGPointMake(size.width/2, size.height/2);
[spaceship setScale:.3];
[self addChild:spaceship];
}
return self;
}
source
share