本文介绍了使用带有 SKScene 子类的 sks 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴看到您在 Xcode 6 中使用 SKS 编辑器可以做的所有事情.我可以在 sks 编辑器中布置一些精灵、灯光、法线等.然后我可以像这样在我的视图控制器中加载 sks 文件:

I'm pretty exicted to see all that you can do with the SKS editor in Xcode 6. I'm able to lay out some sprites, lights, normals, etc... in the sks editor. I can then load the sks file in my view controller like such:

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

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

        NSString *scenePath = [[NSBundle mainBundle] pathForResource:@"MyScene" ofType:@"sks"];
        SKScene *scene = [NSKeyedUnarchiver unarchiveObjectWithFile:scenePath];
        [skView presentScene:scene];
        self.scene = scene;
    }
}

但是我看不到如何将任何代码绑定到 sks 文件.例如,我有 MyScene.sks(名为 MyScene).然后我创建了类文件 MyScene.h 和 MyScene.m,以便我可以通过编程方式控制场景.但是,当我放置断点 MyScene.m 时,它们都没有到达.

However I don't see how to bind any code to the sks file. For instance I have MyScene.sks (named MyScene). I then created class files MyScene.h and MyScene.m so that I can programatically control the scene. However when I place breakpoints MyScene.m, none of them are ever reached.

如何将 MyScene.h/m 绑定到 MyScene.sks?

How do I bind MyScene.h/m to MyScene.sks?

问这个问题的另一种方式.在 MyScene.sks 中,我有一个背景纹理(在编辑器中命名为 background)和一个灯光(在编辑器中命名为 light1).我想以编程方式移动灯光.我希望我能够创建 MyScene.h/m,覆盖 touchesBegan/touchesMoved,并调整灯光的位置.当我添加此代码时,它从未执行过.为什么?

Another way to ask this. In MyScene.sks, I have a background texture (named background in the editor) and a light (named light1 in the editor). I want to programatically move the light. I would expect I'd be able to create MyScene.h/m, override touchesBegan/touchesMoved, and adjust the position of the light. When I add this code, it's not ever executed. Why?

推荐答案

默认SK游戏模板中给出了场景文件与.sks文件的连接示例.GameViewController 为此有一个类别SKScene (Unarchive)".

Example of connection of your scene files with .sks file is given in the default SK game template. GameViewController has a category "SKScene (Unarchive)" for this.

+ (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;
}

您可以在自己的类中实现相同的类别.

You can implement the same category in your own class.

然后你只需调用:

MyScene *scene = [MyScene unarchiveFromFile:@"MyScene"];

这篇关于使用带有 SKScene 子类的 sks 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 23:52