从 SKScene 返回到视图控制器 [英] Go back to view controller from SKScene
问题描述
因此,当您使用 SpriteKit 模板创建项目时.你有你的视图控制器和你的 SKScene.
So when you create a project using the SpriteKit template. You have your View controller and your SKScene.
在我的视图控制器中,我使用默认提供的代码启动我的游戏并呈现场景.
From my view controller I start my game with the code given by default and present the scene.
在我的 TCAViewcontroller.m 中
In my TCAViewcontroller.m
- (IBAction)startGame:(id)sender {
NSLog(@"Start Game triggered");
mainPageImage.hidden = 1;
// Configure the view.
// Configure the view after it has been sized for the correct orientation.
SKView *skView = (SKView *)self.view;
if (!skView.scene) {
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
TCAMyScene *theScene = [TCAMyScene sceneWithSize:skView.bounds.size];
theScene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:theScene];
}
}
当用户在游戏中失败时,我想关闭场景并回到我拥有的视图控制器.我似乎无法通过搜索找到任何返回原始视图控制器的内容,只是将游戏推到场景中.但我不想推送到另一个场景,只需关闭当前场景并返回到我的 TCAViewController.请使用代码回答以进行澄清谢谢
When the user loses in the game I would like to dismiss the scene and go back to my view controller I have. I can't seem to find anything with my searches to going back to the original view controller, just pushing to a game over scene. But I don't want to push to another scene, just dismiss the current scene and go back to my TCAViewController. Please answer using code for clarification Thanks
推荐答案
您的场景需要向您的控制器提供一条通信线路以指示已完成.例如,您可以在场景中创建委托协议和相应的属性.一个例子:
Your scene needs to offer a line of communication to your controller to indicate that is finished. You could, for example, create a delegate protocol and corresponding property in your scene. An example:
@protocol TCAMySceneDelegate;
@interface TCAMyScene : SKScene
@property (nonatomic, weak> id<TCAMySceneDelegate> delegate;
@end
@protocol TCAMySceneDelegate <NSObject>
- (void)mySceneDidFinish:(TCAMyScene *)gameScene;
@end
然后,在您的 TCAMyScene
- (void)endTheGame {
// Other game-ending code
[self.delegate mySceneDidFinish:self];
}
在您的视图控制器中,将自己设置为场景的代理并实现该方法:
In your view controller, set itself as the delegate for your scene and implement the method:
- (IBAction)startGame:(id)sender {
// Other code
TCAMyScene *theScene = [TCAMyScene sceneWithSize:skView.bounds.size];
theScene.scaleMode = SKSceneScaleModeAspectFill;
theScene.delegate = self;
// Other code
}
- (void)mySceneDidFinish:(TCAMyScene *)myScene {
// logic for dismissing the view controller
}
这篇关于从 SKScene 返回到视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!