ios – 回到SKScene查看控制器

因此,当您使用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
}
https://stackoverflow.com/questions/21916702/go-back-to-view-controller-from-skscene

转载注明原文:ios – 回到SKScene查看控制器