簡體   English   中英

返回查看SKScene的控制器

[英]Go back to view controller from 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM