SpriteKit 暂停和恢复 SKView [英] SpriteKit Pause and Resume SKView

查看:17
本文介绍了SpriteKit 暂停和恢复 SKView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to Pause and Unpause a Scene in SpriteKit, with 2 Buttons on the same position. While the Scene is running, I want to show the 'Pause' Button. While the Scene is paused, I want to hide the 'Pause' Button and show the 'Play' Button. In SpriteKit you can use self.scene.view.paused which is defined in SpriteKit.

我的代码:

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

}

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

It pauses the Scene, but the there is still the Pause Button, and if I touch the Pause Button again, the Scene resumes. So now only the Images won't change. How can I fix this?

推荐答案

当 SKView 暂停时,您无法更新按钮(或场景中的任何其他内容).在您的 touchesBegan 方法中,您在更新按钮之前暂停了视图(更改顺序将不起作用).您将需要返回到运行循环,以便在暂停游戏之前更新您的按钮.这是一种方法:

You can't update the button (or anything else in the scene) while the SKView is paused. In your touchesBegan method, you are pausing the view before updating the button (changing the order won't work). You will need to return to the run loop so your button is updated before pausing the game. Here's one way to do that:

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

This calls a method to pause the view after a short delay. Add this after your [self Resume] statement in touchesBegan, and delete self.scene.view.paused = YES.

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

此方法暂停 SKView.将此添加到您的 MyScene.m

This method pauses the SKView. Add this to your MyScene.m

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

这篇关于SpriteKit 暂停和恢复 SKView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆