user3545063
user3545063

Reputation: 811

Pausing game in Sprite Kit: Why doesn't this work?

So I added a pause button to my game and the freezing and unfreezing of the view works. However I want it to display a message saying "PAUSE" over the screen if the view is currently frozen. But if I'm touching my pause button it doesn't display the label. It's really strange, because I discovered that if I'm turning my device to landscape mode, the "PAUSE" message appears. (and vice versa too, so if I touch the button in landscape mode and turn my device to portrait, the pause label appears) Can anyone figure this out?

BTW: I also tried out different methods to do this, like initializing the pause label as soon as the scene starts and hide/reveal it whenever needed. This didn't work either. I also tried to do it with an if-statement in the update method ( if the scene is paused => reveal the pause label) this didn't work either.

I asked this question before on here and the apple devforums but no one could solve this problem yet. Someone thought I would present the scene in viewwilllayoutsubviews, which would explain the portrait-landscape behavior, but I never used that method in my whole application code.

 -(void)gameScene{


 //Pause
    pauseButton = [SKSpriteNode spriteNodeWithImageNamed:@"pause.jpg"];
    pauseButton.position = CGPointMake(screenWidth/2, screenHeight-80);
    pauseButton.zPosition = 5;
    pauseButton.name = @"pauseButton";
    pauseButton.size = CGSizeMake(80, 80);

    [self addChild:pauseButton];

    //Pause Label (wenn Pause gedrückt wird)
    pauseLabel = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    pauseLabel.text = @"PAUSE";
    pauseLabel.fontSize = 70;
    pauseLabel.position = CGPointMake(screenWidth/2, screenHeight/2);
    pauseLabel.zPosition = 5;
    pauseCount = 1;
}




-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

//Pause Button
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:location];


    if ([node.name isEqualToString:@"pauseButton"]) {            

        pauseCount++;


        if (pauseCount%2 == 0) {
            self.scene.view.paused = YES;
            [self addChild:pauseLabel];

        }

        else{
            self.scene.view.paused = NO;
            [pauseLabel runAction:remove];
        }


    }

}

Upvotes: 2

Views: 752

Answers (1)

fuzzygoat
fuzzygoat

Reputation: 26223

I had a similar problem with pausing and found that the problem related to the fact that although the pauseLabel was getting added to the node tree the scene was pausing before it was displayed. The solution I used was to use SKAction to run the label add and pause in sequence.

SKAction *pauseLabelAction = [SKAction runBlock:^{
   [[self scene] addChild:pauseLabel];
}];

SKAction *delayAction = [SKAction waitForDuration:0.1]; // Might not be needed.

SKAction *pauseSceneAction = [SKAction runBlock:^{
   [[[self scene] view] setPause:YES];
}];

SKAction *sequence = [SKAction sequence:@[pauseLabelAction, delayAction, pauseSceneAction]];
[self runAction:sequence];

I have not tested this but there might also need to be a small delay in there to give the label time to display before the pause is fired.

Upvotes: 2

Related Questions