返回查看SKScene的控制器 [英] Go back to view controller from SKScene
问题描述
因此,当您使用SpriteKit模板创建项目时。你有你的View控制器和你的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
然后,在的TC中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屋!