SpriteKit 自动恢复和自动暂停 iOS8 [英] SpriteKit Autoresumes and auto pauses iOS8

查看:21
本文介绍了SpriteKit 自动恢复和自动暂停 iOS8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景在 applicationWillResignActive 上自动暂停,并在 applicationDidBecomeActive 运行时自动取消暂停.我希望通过 nsnotification 在 applicationWillResignActive 上暂停场景,而不是在运行 applicationDidBecomeActive 时自动恢复.有任何想法吗?提前致谢.

The scene auto pauses on applicationWillResignActive and auto unpauses when applicationDidBecomeActive is run. I am looking to either have the scene pause on applicationWillResignActive via the nsnotification and not auto resume when applicationDidBecomeActive is run. Any ideas? Thanks in advance.

AppDelegate

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];
}

游戏视图控制器

- (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

I'm pretty sure it's a bug in spritekit. No matter what you do, the game will unpause itself in applicationDidBecomeActive

我在这里问了同样的问题.在应用启动/退出时暂停 spritekit 游戏 ..iOS8 你必须继承 SKScene 并覆盖 paused 属性才能使其工作.很奇怪你必须这样做.它真的不应该有这么多问题,但这是我可以让我的游戏保持暂停的唯一方法

I asked the same question here. pausing spritekit game on app launch / exit .. iOS8 You have to subclass SKScene and override the paused property to get it to work. It's weird you have to do that. It really shouldnt have so many problems, but thats the only way I could get my game to stay paused

好的,我将代码翻译成objective-c.我希望这对你有用,因为我的 Objective-c 比我预期的要生锈.

okay, I translated the code to objective-c. I hope this is useful to you because my objective-c was rustier than i expected.

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

这篇关于SpriteKit 自动恢复和自动暂停 iOS8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆