ios spritekit教學 從SKScene展示一個UIViewController



swift spritekit入門 (1)

我發現了一個比大多數解決方案簡單得多的解決方案:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"PurchasedVC"];
//Call on the RootViewController to present the New View Controller
[self.view.window.rootViewController presentViewController:vc animated:YES completion:nil];

如果你在Storyboard中創建一個segue並給它一個標識符,我還想出了一個更簡單的一行:

[self.view.window.rootViewController performSegueWithIdentifier:@"PurchasedViewController" sender:self];

我試圖從SKScene提出一個UIViewController,我不知道如何做到這一點。 Google搜索提供的大部分內容是如何從SKScene展示共享屏幕,但這不是我想要做的。 只是一個常規的視圖控制器,將有一個按鈕和一個標籤(也可能是一個圖像)。

這是我的Intro.m中的代碼。 這不是遊戲場景,只是帶有標題,播放按鈕和刪除廣告按鈕的介紹屏幕:

#import "MyScene.h"
#import "Intro.h"
#import "PurchasedViewController.h"

@interface PurchasedViewController ()

-(id)initWithSize:(CGSize)size {

   if (self = [super initWithSize:size]) {

  .....

   SKSpriteNode *removeAds = [SKSpriteNode spriteNodeWithImageNamed:@"removeAds"];
     removeAds.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame)/6);
     removeAds.zPosition = 1;
     removeAds.name = @"RemoveAds";
    [self addChild:removeAds];
  }

 - (void)performTouches:(NSSet *)touches {    

  for (UITouch *touch in touches) {
    CGPoint pointInSKScene = [self.view convertPoint:[touch locationInView:self.view] toScene:self];
    SKNode *touchedNode = [self nodeAtPoint:pointInSKScene];

    if ([touchedNode.name isEqualToString:@"RemoveAds"]) {
        [self runAction:[SKAction playSoundFileNamed:@"enter.mp3" waitForCompletion:NO]];

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
        UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"PurchasedViewController"];
        [self presentViewController:vc animated:YES completion:nil];
      }
    }
  }

Xcode給我的錯誤是“沒有可見的@interface為介紹聲明”presentViewController:動畫:完成“。

我有點難過,因為我還沒有試圖從SKScene提出一個UIViewController之前,無法在網上找到解決方案。





sprite-kit