SpriteKit场景暂停

时间:2014-09-24 12:57:02

标签: ios objective-c sprite-kit

我试图在当前场景进入后台以及每当打开iAD时暂停当前场景。

使用我的代码,它的行为很奇怪,就像在场景取消后,它会运行在暂停一秒钟时应该完成的所有操作。我意识到这种情况最有可能发生,如果我暂停并且非常快速地让场景不安,所以我尝试添加NSTimer,但最后它不起作用。

此外,它不会暂停整个场景。例如,我有两个节点,运行动作。第一个节点暂停,但第二个节点没有暂停。

编辑:感谢LearnCocos2D,您无法暂停更新:方法。而不是skView.scene.paused = YES;你写skView.paused = YES;

我的代码:

AppDelegate.m

- (void)applicationWillResignActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[NSTimer scheduledTimerWithTimeInterval:5.0
                                 target:self
                               selector:@selector(UnpauseGame)
                               userInfo:nil
                                repeats:NO];
}

- (void)UnpauseGame
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
}

ViewController.m

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{

SKView *skView = (SKView *)self.view;
skView.scene.paused = YES;

return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
SKView *skView = (SKView *)self.view;
skView.scene.paused = NO;
}

不会在MyScene.m

中暂停的SpriteNode
-(SKSpriteNode *)createBackground {

Baumstamm = [SKSpriteNode spriteNodeWithImageNamed:@"BaummitLeiter.png"];
Baumstamm.size = CGSizeMake(280, 570);
Baumstamm.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
Baumstamm.zPosition = 2;
Baumstamm.name = BACK_GROUND;

return Baumstamm;
}

-(void)update:(CFTimeInterval)currentTime {

[self enumerateChildNodesWithName:BACK_GROUND usingBlock:^(SKNode *node, BOOL *stop) {

node.position = CGPointMake(node.position.x, node.position.y - 5);

if (node.position.y < -(node.frame.size.height + 100))
{
    [node removeFromParent];
}
}];

if (self.currentBackground.position.y < (self.frame.size.height / 2))
{
SKSpriteNode *temp = [self createBackground];
temp.position = CGPointMake(self.frame.size.width / 2, self.currentBackground.position.y + self.currentBackground.frame.size.height);
[self addChild:temp];
self.currentBackground = temp;
}

1 个答案:

答案 0 :(得分:0)

对于在这样的场景中暂停,我在我的更新方法中添加了一个条件,确保场景在执行所有内容之前不会暂停。这样,您可以有选择地选择暂停期间发生的事情以及不会发生的情况。