SpriteKit暂停和恢复SKView

时间:2014-09-01 16:09:40

标签: ios objective-c sprite-kit skview

我想在SpriteKit中暂停和取消暂停场景,同一位置有2个按钮。 当场景正在运行时,我想显示“暂停”按钮。 当场景暂停时,我想隐藏“暂停”按钮并显示“播放”按钮。 在SpriteKit中,您可以使用SpriteKit中定义的self.scene.view.paused

我的代码:

@implementation MyScene {

SKSpriteNode *PauseButton;
SKSpriteNode *PlayButton;

}

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {

[self Pause];

}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

SKNode * Node = [self nodeAtPoint:location];

if([Node.name isEqualToString:@"PauseButton"]){

    self.scene.view.paused = YES;

    [PauseButton removeFromParent];
    [self Resume];
}

if([Node.name isEqualToString:@"PlayButton"]){

    self.scene.view.paused = NO;

    [PlayButton removeFromParent];
    [self Pause];
}
}

-(void)Pause{

PauseButton = [SKSpriteNode spriteNodeWithImageNamed:@"Pause.png"];
PauseButton.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 1.04);
PauseButton.zPosition = 3;
PauseButton.size = CGSizeMake(40, 40);
PauseButton.name = @"PauseButton";

[self addChild:PauseButton];

}

-(void)Resume{

PlayButton = [SKSpriteNode spriteNodeWithImageNamed:@"Play.png"];
PlayButton.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 1.04);
PlayButton.zPosition = 3;
PlayButton.size = CGSizeMake(60, 60);
PlayButton.name = @"PlayButton";
[self addChild:PlayButton];

}

暂停场景,但仍有暂停按钮,如果再次触摸暂停按钮,场景将恢复。所以现在只有图像不会改变。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

暂停SKView时,您无法更新按钮(或场景中的任何其他内容)。在touchesBegan方法中,您在更新按钮之前暂停视图(更改已赢得的订单)。您需要返回运行循环,以便在暂停游戏之前更新按钮。这是一种方法:

这会调用一种方法,在短暂延迟后暂停视图。在[self Resume]中的touchesBegan语句后添加此项,然后删除self.scene.view.paused = YES

    [self performSelector:@selector(pauseGame) withObject:nil afterDelay:1/60.0];

此方法暂停SKView。将其添加到MyScene.m

- (void) pauseGame
{
    self.scene.view.paused = YES;
}