0

我正在制作像 Flappy Bird 这样的游戏。如何呈现来自 SKScene 的 UIViewController?首先,我告诉我的环境

  • Mac OS X 10.9
  • Xcode 5.0.2
  • 在我的项目中添加了 Sprite Kit(framework)、social.framework(framework)

我的目标是在游戏结束时显示“分享”按钮。点击分享按钮图像应该会显示一个 SLComposeViewController (Twitter Share)。场景的内容不应该改变。
我想解决以下问题并将显示从 GameOverScene 更改为由 social.framework 组成的 tweetSheet(display)。

问题

[self presentViewController:tweetSheet animated:YES completion:nil];
//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:


我的编码文件如下(我提取了部分重要代码)。

视图控制器.h

import <UIKit/UIKit.h>
import <SpriteKit/SpriteKit.h>
import <iAd/iAd.h>

@interface ViewController : UIViewController<ADBannerViewDelegate><br>

@end

GameOverScene.h

#import <SpriteKit/SpriteKit.h> 

@class SpriteViewController;

@interface GameOverScene : SKScene {
}
@end

GameOverScene.m

#import "GameOverScene.h"
#import "NewGameScene.h"
#import "MainScene.h"
#import <Social/Social.h>
@implementation GameOverScene {
   //The twitter button 
    SKSpriteNode *_twitterbutton;
}
- (id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size]) {
        //Creating the twitterbutton with the twitterbutton image from Images.xcassets
        _twitterbutton = [SKSpriteNode spriteNodeWithImageNamed:@"twitterbutton"];
        [_twitterbutton setSize:CGSizeMake(50, 50)];
        [_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)];
        //Adding the twitter button
        [self addChild:_twitterbutton];
        //Again, this is important, otherwise we can't identify what button is pressed
        _twitterbutton.name = @"twitterbutton";
        [_twitterbutton setPosition:CGPointMake(self.size.width/2, self.size.height/5 + 50)]
    }
    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Same as in NewGameScene menu
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    //Is the twitter button touched?
    if([node.name isEqualToString:@"twitterbutton"]){
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
            SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            [tweetSheet setInitialText:@"TestTweet from the Game !!"];
            [self presentViewController:tweetSheet animated:YES completion:nil];
     **//Error:No visible @interface for 'GameOverScene' declares the selector "presentViewController":animated:completion:**
         } 
    }

视图控制器.m

#import "ViewController.h"
#import "NewGameScene.h"
@implementation ViewController
//Loads the view onto our main class
- (void)loadView
{
  self.view  = [[SKView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
//Executes when view finishes loading
- (void)viewWillLayoutSubviews
{
  [super viewDidLoad];

  //Set the resize mode to flexible width and height
  [self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];

  //Create our view from our original view
  //Make sure to leave originalContentView in, otherwise the app will crash
  SKView *skView = (SKView *)self.originalContentView;

  //We create a new NewGameScene according to the current dimensions
  SKScene *scene = [NewGameScene sceneWithSize:skView.bounds.size];

  //Create a transition class with animation type fade and a duration of .4 seconds
  SKTransition *transition = [SKTransition fadeWithDuration:.4];

  //Present the menu view (NewGameScene) with our fade in transition
  [skView presentScene:scene transition:transition];
}

@end
4

1 回答 1

2

您不能从 SKScene 中呈现 viewController,因为它实际上只在 SKView 上呈现。您需要一种向 viewController 发送消息的方法,然后 viewController 将呈现 viewController。为此,您可以使用委托

将以下协议定义添加到您的 SKScene 的 .h 文件中:

@protocol sceneDelegate <NSObject>
-(void)showShareScreen;
@end

并在接口中声明一个委托属性:

@property (weak, nonatomic) id <sceneDelegate> delegate;

然后,在您要显示共享屏幕的位置,而不是该行:

[self presentViewController:tweetSheet animated:YES completion:nil];

使用这一行:

[self.delegate showShareScreen];

现在,在 viewController 的 .h 文件中,实现协议:

@interface ViewController : UIViewController <sceneDelegate>

并且,在您的 .m 文件中,在呈现场景之前添加以下行:

scene.delegate = self;

然后在此处添加以下方法:

-(void)presentShareScreen
{

    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
        composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"TestTweet from the Game !!"];
        [self presentViewController:tweetSheet animated:YES completion:nil];

    }
}

另一种方法是使用NSNotificationCenter

保留-presentShareScreen上一个替代方案中描述的方法。

将 viewController 作为监听器添加到它的-viewDidLoad方法中的通知中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentShareScreen) name:@"showShareScreen" object:nil];

然后,在要显示此 viewController 的场景中,使用以下行:

[[NSNotificationCenter defaultCenter] postNotificationName:@"showShareScreen" object:nil];

于 2014-03-15T13:02:15.997 回答