有没有阻止SpriteKit在进入前景/变为活动时自动取消暂停场景?
我设置paused = true
并希望它在发送到后台后再次变为活动状态时保持不变。
我应该补充一点,我是在迅速做到这一点,尽管我不希望这方面的行为有所不同。
答案 0 :(得分:5)
不确定它在目标C中是否相同,但在快速中我必须"覆盖" SKView在幕后调用的回调函数
func CBApplicationDidBecomeActive()
{
}
此功能导致暂停重置。
(请注意不应用覆盖关键字)
在某些情况下,如果您只想保留暂停状态,请改为创建一个新变量并覆盖isPaused方法。
class GameScene:SKScene
{
var realPaused = false
{
didSet
{
isPaused = realPaused
}
}
override var isPaused : Bool
{
get
{
return realPaused
}
set
{
//we do not want to use newValue because it is being set without our knowledge
paused = realPaused
}
}
}
答案 1 :(得分:3)
Pinxaton你是对的,但你可以通过添加一个小延迟暂停应用程序
(void)theAppIsActive:(NSNotification *)note
{
self.view.paused = YES;
SKAction *pauseTimer= [SKAction sequence:@[
[SKAction waitForDuration:0.1],
[SKAction performSelector:@selector(pauseTimerfun)
onTarget:self]
]];
[self runAction:pauseTimer withKey:@"pauseTimer"];
}
-(void) pauseTimerfun
{
self.view.paused = YES;
}
答案 2 :(得分:2)
这个问题可能已经很老了,但我今天遇到了同样的问题,我想我已经找到了一个很好的解决方案:
在AppDelegate中,我执行以下操作:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
if ([view.scene isKindOfClass:[GameScene class]])
{
XGGameScene *scene = (GameScene *)view.scene;
[scene resumeGame];
}
}
然后在GameScene
类本身,我更新BOOL以反映应用程序刚从后台恢复:
- (void)resumeGame
{
// Do whatever is necessary
self.justResumedFromBackground = YES;
}
最后,在update:循环中,我运行以下命令:
- (void)update:(NSTimeInterval)currentTime
{
// Other codes go here...
// Check if just resumed from background.
if (self.justResumedFromBackground)
{
self.world.paused = YES;
self.justResumedFromBackground = NO;
}
// Other codes go here...
}
希望这有帮助!
答案 3 :(得分:1)
您应该使用您的app委托,特别是applicationDidBecomeActive方法。在该方法中,发送SpriteKit视图侦听的通知。
因此在applicationDidBecomeActive方法中,您的代码应如下所示:
// Post a notification when the app becomes active
[[NSNotificationCenter defaultCenter] postNotificationName:@"appIsActive" object:nil];
现在,在SKScene文件的didMoveToView方法中输入以下内容:
// Add a listener that will respond to the notification sent from the above method
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(theAppIsActive:)
name:@"appIsActive" object:nil];
然后只需将此方法添加到您的SKScene文件中:
//The method called when the notification arrives
(void)theAppIsActive:(NSNotification *)note
{
self.view.paused = YES;
}
答案 4 :(得分:1)
我对Knight0fDragon的解决方案进行了一些调整,以使其适用于我。这使得isPaused将始终等于realPaused。为了暂停游戏,应仅更改“ realPaused”变量,isPaused变量也会自动更改。
var realPaused: Bool = false {
didSet {
self.isPaused = realPaused
}
}
override var isPaused: Bool {
didSet {
if (self.isPaused != self.realPaused) {
self.isPaused = self.realPaused
}
}
}
不幸的是,当应用程序在后台运行时,这可以防止场景暂停。为了防止这种情况,我将条件更改为:“ self.isPaused!= self.realPaused && self.isPaused == false”,以便当应用程序置于后台时场景仍会自动暂停,但只会重新如果realPaused也为true,则激活:
var realPaused: Bool = false {
didSet {
self.isPaused = realPaused
}
}
override var isPaused: Bool {
didSet {
if (self.isPaused == false && self.realPaused == true) {
self.isPaused = true
}
}
}