【发布时间】:2014-07-15 03:48:05
【问题描述】:
我在一个游戏中成功实现了按下主页按钮时的暂停功能。在具有主场景的视图控制器中,我暂停使用:
- (void)appWillEnterBackground{
SKView *skView = (SKView *)self.view;
skView.paused = YES;
bubbleOn=NO; //turns bubble spawn off
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(appWillEnterForeground)
name:UIApplicationDidBecomeActiveNotification
object:NULL];
}
要取消暂停,
- (void)appWillEnterForeground{
SKView * skView = (SKView *)self.view;
skView.paused=NO;
bubbleOn=YES; Allows recursive method to run until bubbleOn = YES
[NSTimer scheduledTimerWithTimeInterval:slowMo target:scene selector:@selector(spawnNew) userInfo:nil repeats:NO]; //Recursive spawn method
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(appWillEnterBackground)
name:UIApplicationWillResignActiveNotification
object:NULL];}
在我的游戏结束并呈现一个新场景(结束场景)之前,这很好用。结束场景显示分数后,用户可以再次点击以重新开始并呈现主场景。主场景的 initWithSize 方法开始递归方法 spawnNew。如果应用程序进入后台,场景会暂停并 spawnNew 停止。
但是当应用程序进入前台时,场景确实恢复了,但是 spawnNew 方法不起作用。它被调用并输出正确的 NSLog 消息,但该方法不会产生气泡节点。
spawnNew 方法在我的主场景的实现中:
-(void) spawnNew{
if (bubbleOn==YES){
bubble = [SKSpriteNode spriteNodeWithImageNamed:ballName];
bubble.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:bubble.size.width/2];
...
//bubble properties
...
[self addChild:bubble];
NSLog(@"Spawn!");
[NSTimer scheduledTimerWithTimeInterval:slowMo target:self selector:@selector(spawnNew) userInfo:nil repeats:NO];
return;
} else{
return;
}
我现在没有想法!有什么建议么?
【问题讨论】:
-
不要使用 nstimer,计时器不会暂停
标签: ios objective-c sprite-kit