有没有办法防止SpriteKit在进入前台/变为活动状态时自动取消暂停场景?
我设置了paused = true
,希望即使应用程序被发送到后台后再次变为活动状态,它仍保持不变。
我应该补充说,我是用swift编写的,尽管我不认为在这方面行为会有所不同。
有没有办法防止SpriteKit在进入前台/变为活动状态时自动取消暂停场景?
我设置了paused = true
,希望即使应用程序被发送到后台后再次变为活动状态,它仍保持不变。
我应该补充说,我是用swift编写的,尽管我不认为在这方面行为会有所不同。
不确定Objective C是否相同,但是在Swift中,我必须“重写”SKView在幕后调用的回调函数。
func CBApplicationDidBecomeActive()
{
}
这个函数会导致暂停被重置。
(注意,不应用override关键字)
在某些情况下,如果您只想保留pause状态,请创建一个新变量,并覆盖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
}
}
}
CBApplicationDidBecomeActive
。如果它们有帮助,@pinxaton你应该接受其中一个答案。 - muZero- (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;
}
- (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...
}
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;
}
// Post a notification when the app becomes active
[[NSNotificationCenter defaultCenter] postNotificationName:@"appIsActive" object:nil];
// 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;
}
UIApplicationDidBecomeActiveNotification
了,所以不需要发布你自己的。 - bio
paused = false
不会取消暂停场景吗? - BergQuester