且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 SKScene 返回到视图控制器

更新时间:2021-07-20 03:24:47

您的场景需要向您的控制器提供一条通信线路以指示已完成.例如,您可以在场景中创建委托协议和相应的属性.一个例子:

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
}