本文介绍了是什么导致我的 SKAction 计时器行为异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个场景,其中有这个方法 createSceneContents,它在 didMoveToView 被调用时被调用.在这个方法中,我有一些创建场景的东西,包括一个生成这样的节点的计时器:

Okay, I have a scene in which I have this method, createSceneContents, that gets called when didMoveToView gets called. In this method I have a couple of things that create the scenes, including a timer that spawns nodes like this:

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 节点只是一个我将所有游戏元素添加到其中的节点.

enemyData is an object of my enemy class which basically just adds a SKSpriteNode to the scene. The world node is simply a node that I add all the game elements to.

这是在 spawningEnemy 方法中发生的事情:

This is what happens in the spawningEnemy method:

-(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 并设置一些属性.它还将其添加到世界中.

This just gets an SKSpriteNode from the enemy class and sets some properties. It also adds it to the world.

我还有 4 种方法可以暂停、恢复、重新启动和游戏结束游戏:

I also have 4 methods that pause, resume, restart and game-over the game:

-(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;

}

这些方法还有更多,但这才是真正重要的.

There's more in these methods, but this is all that really matters.

现在问题来了:这一切正常,直到我退出应用程序.当返回应用程序时,游戏会自动调用暂停方法,但随后我点击了重启或恢复,spawningEnemy 方法没有被调用.(如您所见,我检查了 NSLog 消息)

Now here's the problem: This all works fine until I exit the app. When returning to the app, the game automatically calls the pause method, but then I hit restart or resume, the spawningEnemy method is not being called. (I checked with an NSLog message as you can see)

可能是什么原因造成的?

What might have caused this?

推荐答案

我已经尝试了下面的代码并且它有效.当应用退出活动时,生成停止,并在应用再次变为活动时重新启动.

I have tried the below code and it works. The spawning stops when the app resigns active and starts itself again once the app becomes active again.

您不需要手动包含要暂停的代码,因为 SpriteKit 在退出活动时会自行暂停,但我包含它是为了展示如何在 AppDelegate 和 SKScene 之间进行通信.

You do not need to manually include code to pause as SpriteKit will pause itself when resigning active but I included it to show how to communicate between the AppDelegate and 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

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];
}

这篇关于是什么导致我的 SKAction 计时器行为异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:51