didBecomeActive un-pauses游戏

时间:2014-10-12 02:44:55

标签: ios iphone sprite-kit appdelegate

我正在使用willResignActive通知暂停我的游戏,它似乎暂停游戏,但是当调用didBecomeActive时,它似乎会自行暂停。

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

- (void) applicationWillResign {

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

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

1 个答案:

答案 0 :(得分:2)

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

1)使用名为stayPaused ...

的布尔值定义SKView子类
    @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)在故事板中,将视图的类更改为MyView

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

4)添加通知程序以设置stayPaused标志

    @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中,发布通知以在应用变为活动时设置停留暂停标志

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