好吧,我有一个场景,其中我有这个方法,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;
}
这些方法还有很多,但这才是真正重要的。
问题来了: 在我退出应用程序之前一切正常。返回应用程序时,游戏会自动调用暂停方法,但当我点击重启或恢复时,spawningEnemy 方法没有被调用。 (如您所见,我检查了一条 NSLog 消息)
这可能是什么原因造成的?
最佳答案
我已经尝试了下面的代码并且它有效。当应用程序退出事件状态时,生成停止,并在应用程序再次激活后重新开始。
您不需要手动包含代码来暂停,因为 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];
}
关于ios - 是什么导致我的 SKAction 计时器表现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28253349/