因此,当您使用SpriteKit模板创建项目时。您拥有View控制器和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];
}
}
当用户在游戏中失败时,我想撤消该场景,然后返回我拥有的视图控制器。回到最初的视图控制器,我似乎找不到任何东西,只是将游戏推到了现场。但是我不想推送到另一个场景,只需关闭当前场景并返回到我的TCAViewController。请回答使用代码进行澄清谢谢
最佳答案
您的场景需要向控制器提供一条通讯线,以指示已完成。例如,您可以在场景中创建委托协议和相应的属性。一个例子:
@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
}