SpriteKit 自动恢复和自动暂停 iOS8

Posted

技术标签:

【中文标题】SpriteKit 自动恢复和自动暂停 iOS8【英文标题】:SpriteKit Autoresumes and auto pauses iOS8 【发布时间】:2014-12-20 02:41:32 【问题描述】:

场景在 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];

游戏视图控制器

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

【问题讨论】:

"暂停视图,暂停世界" 【参考方案1】:

我很确定这是 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

【讨论】:

我编辑了我的答案。我已经对此进行了测试,它对我有用。

以上是关于SpriteKit 自动恢复和自动暂停 iOS8的主要内容,如果未能解决你的问题,请参考以下文章

SpriteKit 暂停和恢复 SKView

SpriteKit - 如何正确暂停和恢复应用程序

Spritekit - 在 didBecomeActive 时保持游戏暂停

Spritekit 通过 touchesBegan 暂停和恢复 SKSpriteNode 的动作

通知和暂停模式

“点击恢复”暂停文本 SpriteKit