• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

是什么意思导致我的 SKAction 计时器行为异常?

用户头像
it1352
帮助1

问题说明

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

正确答案

#1

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgffhk
系列文章
更多 icon
同类精品
更多 icon
继续加载