Objective-C-SpriteKit自动恢复并自动暂停iOS8

codeday· 2019-11-17
本文来自 codeday ,作者 codeday
场景在applicationWillResignActive上自动暂停,并在运行applicationDidBecomeActive时自动取消暂停.我希望通过nsnotification使场景在applicationWillResignActive上暂停,并且在运行applicationDidBecomeActive时不自动恢复.有任何想法吗?提前致谢.

AppDelegate中

 - (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[NSNotificationCenter defaultCenter] postNotificationName:@"backgroundPause" object:nil];
}

GameViewController

- (void)handleNotification:(NSNotification *)notification {

if ([notification.name isEqualToString:@"backgroundPause"]) {
    SKView *skView = (SKView *)self.view;
    skView.scene.paused = YES; //pauses scene

    [self.lblPaused removeFromSuperview];//removes any lingering pause menu items
    [self.lblPausedHelp removeFromSuperview];

    self.lblPaused = [[UILabel alloc] init];
    self.lblPaused.center = CGPointMake(self.view.frame.size.width/2 - 125, self.view.frame.size.height/2 - 40);
    self.lblPaused.text = @"PAUSED";
    [self.lblPaused setFont:[UIFont boldSystemFontOfSize:66]];
    [self.lblPaused sizeToFit];
    self.lblPaused.textColor = [UIColor blackColor];
    [self.view addSubview:self.lblPaused];//adds pause label

    self.lblPausedHelp = [[UILabel alloc] init];
    self.lblPausedHelp.center = CGPointMake(self.view.frame.size.width/2 - 145, self.view.frame.size.height/2 + 40);
    self.lblPausedHelp.text = @"tap anywhere to resume";
    [self.lblPausedHelp setFont:[UIFont boldSystemFontOfSize:26]];
    [self.lblPausedHelp sizeToFit];
    self.lblPausedHelp.textColor = [UIColor blackColor];
    [self.view addSubview:self.lblPausedHelp];//adds pause label
}

}

最佳答案
我很确定这是spritekit中的错误.无论您做什么,游戏都将在applicationDidBecomeActive中暂停自身

我在这里问了同样的问题. pausing spritekit game on app launch / exit .. iOS8您必须继承SKScene的子类,并重写paused属性才能使其正常工作.您必须这样做,这很奇怪.它确实不应该有那么多问题,但这就是我可以让游戏保持暂停状态的唯一方法

编辑:好的,我将代码翻译为Objective-C.我希望这对您有用,因为我的Objective-C比我预期的要生锈.

AppDelegate.m

- (void)applicationWillResignActive:(UIApplication *)application {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"pauseGameScene" object:nil];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"stayPausedNotification" object:nil];
}

SKView子类

@interface MySKView : SKView
- (void) setStayPaused;
@end

@implementation MySKView

bool _stayPaused = false;

- (void) setPaused:(BOOL)paused{
    if (!_stayPaused) {
        super.paused = paused;
    }
    _stayPaused = NO;

}


- (void) setStayPaused{
    _stayPaused = YES;
}

@end

GameViewController

@interface GameViewController : UIViewController

-(void)pauseGame;

@end

@implementation GameViewController

SKScene *_scene;
MySKView *_skView;

-(void)pauseGame{
    _skView.paused = YES;
    _skView.scene.view.paused = YES;
}


- (void)viewDidLoad
{

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pauseGame) name:@"pauseGameScene" object:nil];


    // Configure the view.

    _skView = [[MySKView alloc]initWithFrame:self.view.frame];

    _skView.showsFPS = YES;
    _skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    _skView.ignoresSiblingOrder = YES;

    // Create and configure the scene.
    _scene = [[GameScene alloc]initWithSize:_skView.frame.size];
    _scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [self.view addSubview:_skView];
    [_skView presentScene:_scene];

}

- (void)viewDidAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]addObserver:_skView selector:@selector(setStayPaused) name:@"stayPausedNotification" object:nil];
}


@end