When to use didMoveToView or initWithSize with SpriteKit in xCode 6.4

后端 未结 3 1981
失恋的感觉
失恋的感觉 2021-01-03 03:39

Since xCode was updated to version 6.0, the default method for SpriteKit in \"GameScene\" (the default scene created) changes to:

-(void) didMoveToView:(SKVi         


        
3条回答
  •  长发绾君心
    2021-01-03 04:01

    I think that, for all intents and purposes, there isn't much of a difference between the two methods when it comes to actually setting up your scene and using it. (initWithSize is called when the scene is initialized, while didMoveToView is always called right when the scene is presented by the view). I guess if you really prefer to see the initialization then you could just use the init method without any trouble at all.

    Regarding the .sks file:

    take a look at your view controller implementation file. Right above the v.controller's methods you'll see:

    @implementation SKScene (Unarchive)
    
    + (instancetype)unarchiveFromFile:(NSString *)file {
        /* Retrieve scene file path from the application bundle */
        NSString *nodePath = [[NSBundle mainBundle] pathForResource:fileofType:@"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;
    }
    @end
    

    This is the part that handles the sks. Using the file is totally optional, you aren't forced to do it and it really has no impact on you setting up your scene. However, if you do want to work with it, then you'll find that you'll need to work with this code snippet.

提交回复
热议问题