从SKScene返回查看控制器

所以当你使用SpriteKit模板创build一个项目时。 你有你的视图控制器和你的SKScene。

从我的视图控制器我开始我的游戏默认给出的代码,并呈现场景。

在我的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]; } } 

当用户输了游戏,我想解散场景,然后回到我的视图控制器。 我似乎无法find任何与我的search回到原来的视图控制器,只是推到场景的游戏。 但我不想推到另一个场景,只是解散当前的场景,并返回到我的TCAViewController。 请使用代码澄清谢谢

你的场景需要为你的控制器提供一个通信线路来表示完成。 例如,您可以在场景中创build委托协议和相应的属性。 一个例子:

 @protocol TCAMySceneDelegate; @interface TCAMyScene : SKScene @property (nonatomic, weak> id<TCAMySceneDelegate> delegate; @end @protocol TCAMySceneDelegate <NSObject> - (void)mySceneDidFinish:(TCAMyScene *)gameScene; @end 

然后,在你的TCAMyScene的.m中

 - (void)endTheGame { // Other game-ending code [self.delegate mySceneDidFinish:self]; } 

在你的视图控制器中,将自己设置为场景的代理并实现该方法:

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