Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当应用程序激活时,如何保持SpriteKit场景暂停?_Ios_Sprite Kit_Skscene - Fatal编程技术网

Ios 当应用程序激活时,如何保持SpriteKit场景暂停?

Ios 当应用程序激活时,如何保持SpriteKit场景暂停?,ios,sprite-kit,skscene,Ios,Sprite Kit,Skscene,是否存在防止SpriteKit在进入前景/激活时自动取消场景暂停的方法 我设置了paused=true,希望即使应用程序在被发送到后台后再次激活,它也能保持这样的状态 我应该补充一点,我是在swift中这样做的,尽管我不希望在这方面的行为会有所不同。您应该使用您的应用程序委托,特别是ApplicationIDBecomeactive方法。在该方法中,发送SpriteKit视图侦听的通知 因此,在ApplicationIDBecMeactive方法中,您的代码应该如下所示: // Post a n

是否存在防止SpriteKit在进入前景/激活时自动取消场景暂停的方法

我设置了
paused=true
,希望即使应用程序在被发送到后台后再次激活,它也能保持这样的状态


我应该补充一点,我是在swift中这样做的,尽管我不希望在这方面的行为会有所不同。

您应该使用您的应用程序委托,特别是ApplicationIDBecomeactive方法。在该方法中,发送SpriteKit视图侦听的通知

因此,在ApplicationIDBecMeactive方法中,您的代码应该如下所示:

// 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];
然后只需将此方法添加到场景文件:

//The method called when the notification arrives
(void)theAppIsActive:(NSNotification *)note 
{
    self.view.paused = YES;
}

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;


}

这个问题可能已经很老了,但我今天遇到了同样的问题,我想我已经找到了一个很好的解决方案:

在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];
    }
}
- (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...
}
然后在
游戏场景
类中,我更新了一个BOOL,以反映应用程序刚刚从后台恢复:

- (void)resumeGame
{
    //  Do whatever is necessary

    self.justResumedFromBackground = YES;
}
最后,在update:loop中,我运行以下命令:

- (void)applicationDidBecomeActive:(UIApplication *)application 
{
    SKView *view = (SKView *)self.window.rootViewController.view;

    if ([view.scene isKindOfClass:[GameScene class]]) 
    {
        XGGameScene *scene = (GameScene *)view.scene;
        [scene resumeGame];
    }
}
- (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...
}

希望这有帮助

不确定在objective C中是否相同,但在swift中,我必须“重写”SKView在幕后调用的回调函数

func CBApplicationDidBecomeActive()
{

}
此功能导致暂停被重置

(注意:不应应用override关键字)

在某些情况下,如果您只想保留暂停状态,请改为使用新变量并重写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
    }
  }
}

我对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
        }
    }
}

我不熟悉这些框架,但不会
paused=false
取消场景暂停吗?我的意思是paused=true-更正如果您在进入后台模式之前使用self.scene.view.paused=YES暂停SKView,则当您的应用程序处于活动状态时,它将保持暂停状态。你确定你的视图暂停正确吗?我很肯定我做得正确。在AppDelegate的applicationWillResignActive()函数中,我只是简单地执行“scene.view.paused=true”(swift)。如果我在应用程序激活时不做任何其他操作,我会看到所有暂停的操作都会恢复,并且update()函数仍在被调用并执行它的操作。如果我没有明确地将scene.view.paused设置为“false”,将应用程序带回到前台会使场景保持暂停状态,但这显然不是实际情况。谢谢你的建议。我试过这个,但似乎没有达到预期的效果。当应用程序处于活动状态时,场景会暂停几分之一秒,然后再次恢复。我认为SpriteKit必须在收到并处理通知后取消暂停,可能在不同的线程中?尝试将self.view.paused更改为self.scene.view.paused。您也可以在上述方法中将bool设置为true。然后,在每帧触发一次的更新方法中,只需检查bool,看看是否应该暂停。顺便说一句,已经有了
uiapplicationidbecomeactivationification
,因此无需发布您自己的更新方法。您到底是如何获得此秘密功能的?你是如何实现它的?我通过查看堆栈跟踪找到了它,只需在GameSecene类中完全按照我的方式执行函数,这将覆盖从SKScene调用的函数。您不使用override的原因是,我们无法通过头文件访问此函数,因此我们不知道它是否存在。它没有在游戏场景中被调用,但它确实在SKView扩展和SKView子类中被调用。就像你说的那样,但这似乎很危险。我的意思是,除了设置ispused,这个函数还做什么?我想调用super的实现,然后重置isPaused,但我不知道如何使用super,因为我们在技术上没有覆盖它。不知道它还能做什么,但在技术上它不应该做任何事情。是的,你是对的。哈哈,我甚至在回答中说过,不知道为什么我的大脑会说游戏场景。你可以做的另一个选择是覆盖GameSecene上的isPaused变量,并保留另一个名为isRealPaused的变量,当你想跟踪它时,它会跟踪它。对不起,@mogelbuster忘了标记你,不确定你是否收到通知,是最后一个发送者,我将此应用于我的SKNodes子类,因为它们包含一些我不想从isPaused==true触发的SKActions,而场景仍然没有被触发。