从 UIVIewController 向 SKScene 传递数据

Passing Data to SKScene from UIVIewController

我想要完成的是(至少应该..)非常简单:我想呈现一个 SKScene,其中包含呈现此场景的 UIViewController 中的数据.换句话说,我有一个变量 chapter 声明如下:

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

@interface ViewController : UIViewController

@property (nonatomic) NSInteger chapter;

@end

我已经用日志测试了实际上声明了变量章节(在我的例子中,我只是将它作为 4 进行测试)。

这是我的 ViewController.m 文件以及我如何呈现我的场景:

- (void)viewDidLoad {
    [super viewDidLoad];
    .
    .
    .
    // Configure the view.
    SKView *skView = (SKView *)self.view;

    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

        // Create and configure the scene.
        scene = [OLevel sceneWithSize:CGSizeMake(skView.bounds.size.width, skView.bounds.size.height)];
        scene.scaleMode = SKSceneScaleModeResizeFill;

        // Set the SKScene chapter #
        scene.chapter = self.chapter;     // <<<< NOT WORKING?
        NSLog(@"CHAP TO PASS: %u", self.chapter);
        NSLog(@"CHAP RECEIVED: %u", scene.chapter);

        // Present the scene.
        [skView presentScene:scene];

}

场景定义如下,viewDidLoad 是上面显示的那个:(我有它 subclassed)

@interface ViewController () {
    OLevel *scene;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    .
    .
    .

这是我的OLevel.hclass:

#import <SpriteKit/SpriteKit.h>
#import <GameplayKit/GameplayKit.h>

@interface OLevel : SKScene <SKPhysicsContactDelegate, GKAgentDelegate>

@property (nonatomic) NSInteger chapter;

@end

还有我的OLevel.m*:

- (id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {
        NSLog(@"Creating scene");

        NSLog(@"CHAP: %u", self.chapter);

        [self setUpScene];       
    }

    return self;
}

我很困惑为什么这种方法不起作用。我已将 ViewController 中的 属性 设置为所需的章节值,然后我告诉 Olevel 什么值它是并将它自己的章节 属性 设置为这样的值,然后正确呈现场景。为什么我继续得到0??

2016-07-18 23:52:32.189 TESTAPP[2038:684620] CHAP TO PASS: 4
2016-07-18 23:52:32.193 TESTAPP[2038:684620] CHAP RECEIVED: 0
2016-07-18 23:52:32.195 TESTAPP[2038:684620] Creating scene
2016-07-18 23:52:32.195 TESTAPP[2038:684620] CHAP: 0

我知道这个问题已经得到回答here,使用的方法与我在寻找答案之前最初使用的方法相同,但我还没有让它起作用..

如果有人知道我做错了什么,请帮助我。

您也可以尝试使用 NSNotificationCenter 传递您的数据

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

NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:@"someKey"];
    [[NSNotificationCenter defaultCenter] postNotificationName: @"TestNotification" object:nil userInfo:userInfo];

您使用错误的语法初始化场景,没有类型:

...      
// Create and configure the scene.
OLevel *scene = [OLevel sceneWithSize:CGSizeMake(skView.bounds.size.width, skView.bounds.size.height)];
scene.scaleMode = SKSceneScaleModeResizeFill;

// Set the SKScene chapter #
scene.chapter = self.chapter; 
...

如果你愿意,你也可以使用第

章子类化 init 方法

OLevel.h:

@interface GameScene : SKScene <SKPhysicsContactDelegate, GKAgentDelegate>

@property (nonatomic) NSInteger chapter;

-(id)initWithSize:(CGSize)size chapter:(NSInteger)chapter;
+(id)sceneWithSize:(CGSize)size chapter:(NSInteger)chapter;

@end

OLevel.m:

#import "GameScene.h"
@implementation GameScene

    -(id)initWithSize:(CGSize)size chapter:(NSInteger)chapter {

     if (self = [super initWithSize:size]) {
        self.chapter = chapter;
        //some custom code here
        NSLog(@"Creating scene");
        NSLog(@"CHAP: %ld", (long)self.chapter);

     }
     return self;
    }

    +(id)sceneWithSize:(CGSize)size chapter:(NSInteger)chapter {
          return ((GameScene *)[[self alloc] initWithSize:size chapter:chapter]);
    }

    -(void)didMoveToView:(SKView *)view { 
      ...
    }
}

在这种情况下,您可以将场景创建为:

   // Create and configure the scene.
    scene = [OLevel sceneWithSize:CGSizeMake(skView.bounds.size.width, skView.bounds.size.height) chapter:self.chapter];
    scene.scaleMode = SKSceneScaleModeResizeFill;