使用SKScene子类的sks文件 [英] using sks file with SKScene subclass

查看:202
本文介绍了使用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中,我有一个背景纹理(在编辑器中命名为背景)和一个灯(在编辑器中命名为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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆