场景在 applicationWillResignActive 时自动暂停,并在 applicationDidBecomeActive 运行时自动取消暂停。我希望通过 nsnotification 在 applicationWillResignActive 上暂停场景,而不是在运行 applicationDidBecomeActive 时自动恢复。有任何想法吗?提前致谢。
应用程序代理
- (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
我在这里问了同样的问题。在应用程序启动/退出时暂停 spritekit 游戏.. iOS8 https://stackoverflow.com/questions/26317553/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
游戏视图控制器
@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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)