从UIVIewController

时间:2016-07-19 04:12:35

标签: ios objective-c sprite-kit skscene

我想要完成的是(至少应该......)非常简单:我想提供一个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如上所示:(我有子类)

@interface ViewController () {
    OLevel *scene;
}

@end

@implementation ViewController

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

这是我的 OLevel.h 类:

#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回答了我在寻找答案之前最初使用过的方法,但我还没有让它发挥作用..

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

2 个答案:

答案 0 :(得分:1)

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

...      
// 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;

答案 1 :(得分:0)

您还可以尝试使用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];