【问题标题】:SKScene unarchiveFromFile Method crashing my app?SKScene unarchiveFromFile 方法使我的应用程序崩溃?
【发布时间】:2015-01-25 01:58:00
【问题描述】:

好的,所以我的应用程序在模拟器中运行得非常好,但是当我插入我的设备并尝试在真实设备上运行它时,我收到了这个错误:

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 中的场景设计器

【问题讨论】:

  • 我不确定是什么问题,但您使用的是 SKScene 和 GameScene 类。尝试在视图控制器文件中使用 GameScene 类,看看会发生什么。
  • 什么是文件?文件名是否匹配,包括大小写?

标签: ios objective-c sprite-kit skscene


【解决方案1】:

您似乎想要访问真实设备上不存在的文件。也许您正在访问 iOS 模拟器可以获取(在应用程序支持文件夹中)但真正的 iOS 设备不能获取的目录中的文件。

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

【讨论】:

    【解决方案2】:

    我能够通过避免同时使用取消归档方法来解决此问题。以前该场景是通过以下方式创建的:

        - (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 { [超级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];
    

    }

    【讨论】:

    • 这样,你没有使用GameScene.sks文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多