SKScene unarchiveFromFile方法崩溃了我的应用程序?

时间:2015-01-25 01:58:00

标签: ios objective-c sprite-kit skscene

好的,所以我的应用程序在模拟器中运行得很好但是当我插入我的设备并尝试在真实的东西上运行时,我收到此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[_NSPlaceholderData     
initWithContentsOfFile:options:error:]: nil file argument'

并且由于这种方法:

+ (instancetype)unarchiveFromFile:(NSString *)file {
/* Retrieve scene file path from the application bundle */
NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"];
/* Unarchive the file to an SKScene object */
NSData *data = [NSData dataWithContentsOfFile:nodePath
                                      options:NSDataReadingMappedIfSafe
                                        error:nil];
NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[arch setClass:self forClassName:@"SKScene"];
SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey];
[arch finishDecoding];

return scene; }

更具体地说,它是由“pathForResource:file ofType:@”sks“引起的” 但我不明白为什么这会导致错误,或者我应该如何加载我的场景并使用我的应用程序。如果它有帮助,我使用GameScene.h以编程方式制作我的场景,而不是xcode中的场景设计师

2 个答案:

答案 0 :(得分:0)

看起来您想要访问真实设备上不存在的文件。也许您正在访问iOS模拟器可以获取的目录中的文件(在Application Support文件夹中),但真正的iOS设备不能。

检查代码中是否存在任何可能导致此行为发生的硬编码路径。

答案 1 :(得分:0)

我能够通过避免一起使用unarchiving方法来解决这个问题。以前,场景是使用:

创建的
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = YES;

    // Create and configure the scene.
    GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
    scene.scaleMode = SKSceneScaleModeResizeFill;

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

现在我将其改为此版本,它在真实设备上运行正常:      - (void)viewDidLoad {     [super viewDidLoad];

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

// Create and configure the scene.
SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeResizeFill;

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

}