【发布时间】:2016-07-19 04:12:35
【问题描述】:
我想要完成的是(至少应该是..)非常简单:我想展示一个SKScene,其中包含我在展示这个场景的UIViewController 中拥有的数据。换句话说,我有一个变量chapter,声明如下:
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
@interface ViewController : UIViewController
@property (nonatomic) NSInteger chapter;
@end
我已经用日志测试了变量 chapter 实际上是声明的(在我的情况下,我只是将它测试为 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,方法与我在寻找答案之前最初使用的方法相同,但我还没有让它起作用..
如果有人知道我做错了什么,请帮助我。
【问题讨论】:
标签: ios objective-c sprite-kit skscene