简体   繁体   English

屏幕游戏-SpriteKit

[英]Game Over Screen - SpriteKit

I'm building a simple SpriteKit game. 我正在构建一个简单的SpriteKit游戏。 I would like to add a Game Over Screen, with a button to return to the main screen. 我想添加一个Game Over Screen,并带有一个返回主屏幕的按钮。 But it seems it's not very easy to do with SpriteKit. 但是使用SpriteKit似乎并不容易。

I previously had a "Back" button on the main game view which stayed there when the Game Over view loaded. 我以前在主游戏视图上有一个“返回”按钮,当“游戏结束”视图加载时,该按钮一直停留在该按钮上。 Bur using it and playing again would make the fps drop. Bur使用它并再次播放会使fps下降。 So, its there any way to add a button to a SKScene ? 那么,有什么方法可以向SKScene添加按钮吗? For example like creating a View Controller in Storyboard and adding the button there, then make the Scene appear there... 例如,在情节提要中创建一个View Controller并在其中添加按钮,然后使Scene出现在其中...

If you need any code or anything, just tell me and I'll post it. 如果您需要任何代码或其他任何内容,请告诉我,我将其发布。 Thanks! 谢谢!

The best way is to subclass SKNode to make a button in SpriteKit . 最好的方法是将SKNode子类SKNode以在SpriteKit创建一个按钮。

Simple example: 简单的例子:

@interface MyButton : SKSpriteNode

@property (nonatomic, readonly) SEL action;
@property (nonatomic, readonly, weak) id target;

- (void)setTarget:(id)target action:(SEL)action;

@end

@implementation SKButton

- (void)setTarget:(id)target action:(SEL)action 
{
    _target = target;
    _action = action;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInNode:self.parent];

    if (CGRectContainsPoint(self.frame, touchPoint)) 
    {
        objc_msgSend(_target, _action);
    }
}

@end

Or you can just create a UIButton and add it to the SKView : 或者,您可以只创建一个UIButton并将其添加到SKView

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
button.backgroundColor = [UIColor whiteColor];

[skView addSubview:button];

I haven't experienced any performance hit but my test project is not complex. 我没有遇到任何性能下降的问题,但是我的测试项目并不复杂。

Well, I've found a simple solution that works for me (for now at least). 好吧,我找到了一个对我有用的简单解决方案(至少到目前为止)。 Here it is: http://meghagulati.com/2014/01/20/simple-parse-tutorial-with-sprite-kit-game/ 在这里是: http : //meghagulati.com/2014/01/20/simple-parse-tutorial-with-sprite-kit-game/

The author creates a node and then when the node is touched, the game is loaded again. 作者创建一个节点,然后在触摸该节点时再次加载游戏。 Code can be seen in its website. 可以在其网站上看到代码。

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

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