using sks file with SKScene subclass

£可爱£侵袭症+ 提交于 2019-12-04 02:04:06

问题


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

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.

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

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?


回答1:


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.

Then you just call:

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



回答2:


To reference a node created within your .SKS file use the following SWIFT code.

let sprite = childNodeWithName("NameOfNodeInSceneEditor")

Hope this helps.




回答3:


You don't build a header or methods (.h/.m) file for the .sks file (sprite Kit Scene).

To configure various aspects of this file you use Scene methods. From my experience you are limited to the amount of configuration if you use a .sks file. Creating a particle by hand is better.

You'll want to do GestureRecognizers instead of touchesBegan because of this. In the same location of that you initialize this MyScene.sks you'll need to do something like this:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
NSMutableArray *gestureRecognizers = [NSMutableArray array];
[gestureRecognizers addObject:tapGesture];
[gestureRecognizers addObjectsFromArray:scene.gestureRecognizers];
scene.gestureRecognizers = gestureRecognizers;



回答4:


Here are the steps to access nodes placed in the SKS editor and use them in your code:

  1. Position and name the SKspriteNodes in the SKS editor
  2. Name your " GameWorld" node and enumerate through your scene, replacing SKS nodes with new instance of node(SKSpriteNode) and remove old sks nodes.

See example below:

        self.gameWorld!.enumerateChildNodesWithName("Can") {
        node, stop in

        if let can = node as? SKSpriteNode {

            println("can found")
            self.newCan = SKSpriteNode(imageNamed: "MetalGarbageCan")

            self.gameWorld!.addChild(self.newCan!)
            println("new can added")
            self.newCan!.position = can.position
            self.newCan!.name = can.name!
            self.newCan!.size = can.size
            self.newCan!.zPosition = can.zPosition

            can.removeFromParent()

        }
    }


来源:https://stackoverflow.com/questions/24212771/using-sks-file-with-skscene-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!