In SpriteKit, is it possible to add elements programmatically to the scene, so when .sks is opened, they display? Currently I have my GameScene
(which extends SKScene
) class that opens up already added elements in the scene editor, from the .sks file, using the UnarchiveFromFile method pattern. This method looks like this:
@implementation SKScene (Unarchive)
+ (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;
}
@end
How can I do the opposite thing from the unarchiveFromFile, ie. to go from the code and add sprites to the .sks file. After that, the unarchiveFromFile would be triggered and the rest goes as it is right now. Any suggestions guys? Thanks.
I solved my actual problem! - by just putting in a graph on the scene and getting it in the code. It is a NavigationGraph, and works fine. The programatic adding however didn't work.