gpt4 book ai didi

ios - didBecomeActive 取消暂停游戏

转载 作者:行者123 更新时间:2023-11-28 21:56:36 26 4
gpt4 key购买 nike

我正在使用 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 中暂停它吗?

最佳答案

这是一种在从后台模式返回后保持 View 暂停的方法。这有点 hack,但它确实有效。

1) 使用名为 stayPaused 的 bool 值定义一个 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) 在 Storyboard中,将 View 的类更改为 MyView

3) 在 View Controller 中,将 View 定义为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];
}

关于ios - didBecomeActive 取消暂停游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26321347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com