好的,我有一个场景,其中我有这个方法,createSceneContents,当 didMoveToView 被调用时会被调用。在这种方法中,我有几件事可以创建场景,包括一个生成这样的节点的计时器:
self.spawningSpeed = 1.5;
self.enemyData = [[Enemy alloc]init];
SKAction *wait = [SKAction waitForDuration:1.5];
SKAction *run = [SKAction performSelectorselector(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(@"ausing...");
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 消息)
这可能是什么原因造成的?
Best Answer-推荐答案 strong>
我已经尝试了下面的代码并且它有效。当应用退出事件时,生成停止,并在应用再次变为事件时重新启动。
您不需要手动包含暂停代码,因为 SpriteKit 在退出事件时会自行暂停,但我包含它是为了展示如何在 AppDelegate 和 SKScene 之间进行通信。
AppDelegate.m
- (void)applicationWillResignActiveUIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName"applicationWillResignActive" object:self];
}
- (void)applicationDidBecomeActiveUIApplication *)application {
[[NSNotificationCenter defaultCenter]postNotificationName"applicationDidBecomeActive" object:self];
}
GameScene.m
-(id)initWithSizeCGSize)size {
if (self = [super initWithSize:size]) {
SKAction *wait = [SKAction waitForDuration:1.5];
SKAction *run = [SKAction performSelectorselector(spawningEnemy) onTarget:self];
SKAction *spawnAction = [SKAction repeatActionForever:[SKAction sequence[wait,run]]];
[self runAction:spawnAction withKey"spawn"];
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(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)willMoveFromViewSKView *)view {
// good housekeeping
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
关于ios - 是什么导致我的 SKAction 计时器行为异常?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28253349/
|