好的,我有一个场景,我有这个方法, createSceneContents ,当调用 didMoveToView 时会调用它。在这个方法中,我有几个创建场景的东西,包括产生这样的节点的计时器:
self.spawningSpeed = 1.5;
self.enemyData = [[Enemy alloc]init];
SKAction *wait = [SKAction waitForDuration:1.5];
SKAction *run = [SKAction performSelector:@selector(spawningEnemy) onTarget:self];
self.spawnAction = [SKAction repeatActionForever:[SKAction sequence:@[wait,run]]];
[self.world runAction:self.spawnAction withKey:@"spawn"];
enemyData 是我的敌人类的一个对象,它基本上只是在场景中添加了一个SKSpriteNode。 world 节点只是我将所有游戏元素添加到的节点。
这是 spawningEnemy 方法中发生的事情:
-(void)spawningEnemy {
NSLog(@"spawned");
SKSpriteNode *aNewEnemy = [self.enemyData createEnemyWithSize:self.customUnit andWidth:self.frame.size.width andHeight:self.frame.size.height + self.player.position.y];
aNewEnemy.physicsBody.allowsRotation = NO;
aNewEnemy.physicsBody.categoryBitMask = self.enemyCategory;
aNewEnemy.physicsBody.collisionBitMask = self.enemyCategory | self.playerCategory | self.edgeCategory | self.bottomCategory;
aNewEnemy.physicsBody.contactTestBitMask = self.enemyCategory | self.playerCategory | self.edgeCategory | self.bottomCategory;
[self.world addChild:aNewEnemy];
}
这只是从敌人类中获取SKSpriteNode并设置一些属性。它还将它添加到世界。
我还有4种暂停,恢复,重启和游戏的方法:
-(void)pauseGame {
[self createPauseMenu];
NSLog(@"Pausing...");
self.world.paused = YES;
self.isPaused = YES;
}
-(void)restartGame {
[self removeAllChildren];
[self removeAllActions];
self.enemyData = nil;
self.isPaused = NO;
[self createSceneContents];
}
-(void)resumeGame {
self.isPaused = NO;
self.world.paused = NO;
}
-(void)gameOver {
NSLog(@"Game Over");
self.world.paused = YES;
self.isPaused = YES;
}
这些方法还有更多,但这才是最重要的。
现在问题出现了:这一切都正常,直到我退出应用程序。当返回应用程序时,游戏会自动调用pause方法,但随后我点击重启或恢复, spawningEnemy 方法未被调用。 (我查看了NSLog消息,你可以看到)
可能是什么导致了这个?
答案 0 :(得分:1)
我已经尝试了以下代码并且它有效。当应用程序重新启动时,产生停止,并在应用再次激活后再次启动。
您不需要手动包含要暂停的代码,因为SpriteKit会在重新启动时自动暂停,但我将其包含在内以显示如何在AppDelegate和SKScene之间进行通信。
<强> AppDelegate.m 强>
- (void)applicationWillResignActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName:@"applicationWillResignActive" object:self];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName:@"applicationDidBecomeActive" object:self];
}
GameScene.m
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
SKAction *wait = [SKAction waitForDuration:1.5];
SKAction *run = [SKAction performSelector:@selector(spawningEnemy) onTarget:self];
SKAction *spawnAction = [SKAction repeatActionForever:[SKAction sequence:@[wait,run]]];
[self runAction:spawnAction withKey:@"spawn"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pauseGame)
name:@"applicationWillResignActive"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resumeGame)
name:@"applicationDidBecomeActive"
object:nil];
}
return self;
}
-(void)spawningEnemy {
NSLog(@"spawningEnemy");
}
-(void)pauseGame {
NSLog(@"applicationWillResignActive...");
self.paused = YES;
}
-(void)resumeGame {
NSLog(@"applicationDidBecomeActive...");
self.paused = NO;
}
-(void)willMoveFromView:(SKView *)view {
// good housekeeping
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
答案 1 :(得分:0)
如果您想要暂停的整个场景,那么您应该只暂停包含场景的SKView。这将暂停所有动画,运行循环以及场景与场景中所有节点的交互。
同样经验告诉我,当应用程序进入后台时暂停场景并在返回前台时恢复是一个好习惯。它可以防止以下问题: Sprite Kit & playing sound leads to app termination