ios - 是什么导致我的 SKAction 计时器表现异常?

标签 ios objective-c sprite-kit

好吧,我有一个场景,其中我有这个方法,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/

相关文章:

ios - 如何将文本字段的内容转换为 Xcode 11 (swift) 中的变量?

ios - iOS 13 和 14 上的 NSKeyValuePopPendingNotificationLocal 崩溃

ios - 更新 UILabel 文本会重置 Storyboard吗?

sprite-kit - SpriteKit 内存泄漏更改包含 SKTileMapNodes 的场景

ios - 我如何制作一个 Sequence 来遍历从 UIView 到 View 层次结构的所有祖先( super View )到根?

ios - 如何循环 NSString N 次?

iphone - self.myIvar 和 myIvar 之间有什么不同?

swift - 场景移动时保持中心点

ios - 获取 SKPhysicsBody 以在纹理动画时更新 (Swift)

ios - 希望快速将用户输入发送到网络服务器并加载结果