Search code examples
objective-cphysicsskscene

New scene is created from its frame size but it keeps getting smaller each time. Why?


I am writing a game in which the user must prevent falling objects (rocks) to pass the button of the screen using some plates, each click make a plate at that position but he can only have 5 plates at each time and clicking on sixth point vanished the first plate while creating a new one. That was a brief on how my game mechanics work.

Here is the part of my code that counts falling objects(sprites) using physics and if they became more than 10 a game over message will be shown and the code restarts the game, a new game. to have a new game I need to

-(void)didSimulatePhysics
{
    [self enumerateChildNodesWithName:@"rock" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.y < 0)
        {
            ScoreNum++;
            [node removeFromParent];
            score.text = [NSString stringWithFormat:@"%@ %li",ScoreMSG,(long)ScoreNum];
            if (ScoreNum > 10)
            {
                score.fontSize = 40;
                score.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
                score.text = @"Game Over!";            
                SKScene *spaceshipScene  = [[GameScene alloc] initWithSize:CGRectMake(0, 0,CGRectGetMaxY(self.frame),CGRectGetMaxX(self.frame)).size];                
                printf("%f %f",CGRectGetMidX(self.frame),CGRectGetMidX(self.frame));
                SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.25];
                [self.view presentScene:spaceshipScene transition:doors];
                ScoreNum = 0;         
            }
        }  
    }];
}

GameScene is subclassed from SKScene like this

#import <SpriteKit/SpriteKit.h>
@interface GameScene : SKScene
@end

The code works find but the plates (all of the sprites on the screen) gets squeezed from sides

The following picture shows the results, first time, second time and third time, I needed 10 reputation to include 3 pictures so I had to merge them all into one picture, Here it is. https://www.imageupload.co.uk/images/2015/08/30/123.png Result Sorry, looks like I can not embed an image since I have not enough reputation!

Why each my scene squeezes?


Solution

  • Looks like you set a scaleMode in your viewController.viewDidLoad that handles your game scene, nothing wrong with your physics code (in your question).

    look for something like this scene.scaleMode = SKSceneScaleModeAspectFill usually SKScenes are automatically scaled based on the screen size of the device and it is upto SKScene to provide you a scene.

    Your scenes in your pictures shows that scenes have a complete and full cover but they are scaled.

    It is kind of hard to handle sprite size while do not controling scale. I suggest to get ride of every Scale Mode and remark them all and control the scale your self. if you are not going to zoom in/out as I believe it is the case in your game you just need to reset the scenes scale to 1 each time after each scene transition.