繁体   English   中英

SpriteKit场景暂停

[英]SpriteKit Scene Pause

每当试图在后台或打开iAD时,我都试图暂停当前场景。

使用我的代码,它表现得很奇怪,就像场景暂停后一样,它可以执行暂停一秒钟后应该执行的所有操作。 我意识到如果我非常快速地暂停和放松场景,很可能会发生这种情况,因此我尝试添加一个NSTimer但最终它没有用。

另外,它不会暂停整个场景。例如,我有2个节点,正在运行动作。 第一个节点已暂停,但第二个节点未暂停。

编辑:感谢LearnCocos2D,您不能暂停update:方法。 而不是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中不暂停的MyScene.m

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

为了在这种情况下暂停,我在我的update方法中添加了一个条件,以确保场景在执行所有操作之前不会暂停。 这样,您可以有选择地选择在暂停期间持续发生的事情和没有发生的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM