didBecomeActive 取消暂停游戏 [英] didBecomeActive un-pauses game

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

问题描述

我通过 willResignActive 通知暂停我的游戏,它似乎暂停了游戏,但是当 didBecomeActive 被调用时,它似乎自行取消暂停.

I am pausing my game with a willResignActive notification, and it seems to pause the game, but when didBecomeActive is called, it seems to un-pause on its own.

[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(applicationWillResign)
     name:UIApplicationWillResignActiveNotification
     object:NULL];

- (void) applicationWillResign {

    self.scene.view.paused = TRUE;
    NSLog(@"About to lose focus");
}

如何让它保持暂停状态?我真的需要在我的 AppDelegate 中暂停它吗?

How do I get it to stay paused? Do I actually need to pause it in my AppDelegate?

推荐答案

这是一种在从后台模式返回后保持视图暂停的方法.这有点像黑客,但确实有效.

Here's a way to keep the view paused after returning from background mode. It's a bit of a hack, but it does work.

1) 定义一个名为stayPaused 的布尔值的SKView 子类...

1) Define an SKView subclass with a boolean named stayPaused...

    @interface MyView : SKView

    @property BOOL stayPaused;

    @end

    @implementation MyView

    // Override the paused setter to conditionally un-pause the view
    - (void) setPaused:(BOOL)paused
    {
        if (!_stayPaused || paused) {
            // Call the superclass's paused setter
            [super setPaused:paused];
        }
        _stayPaused = NO;
    }

    - (void) setStayPaused
    {
        _stayPaused = YES;
    }

    @end

2) 在storyboard中,将view的class改为MyView

2) In the storyboard, change the class of the view to MyView

3) 在视图控制器中,将视图定义为 MyView

3) In the view controller, define the view as MyView

4) 添加一个通知器来设置stayPaused标志

4) Add a notifier to set the stayPaused flag

    @implementation GameViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        // Define view using the subclass
        MyView * skView = (MyView *)self.view;

        // Add an observer for a method that sets the stay pause flag when notified
        [[NSNotificationCenter defaultCenter] addObserver:skView selector:@selector(setStayPaused)
                                                     name:@"stayPausedNotification" object:nil];

        ...

5) 在 AppDelegate.m 中,发布通知以在应用程序激活时设置保持暂停标志

5) In AppDelegate.m, post a notification to set the stay paused flag when the app becomes active

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

这篇关于didBecomeActive 取消暂停游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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