Rud*_*udi 12 ios sprite-kit skspritenode sknode
(这适用于XCode 6和iOS 8 beta 4)
喜欢新的SceneKit编辑器.我成功地将.sks文件中的场景加载到自定义SKScene类中.但是,它内部的对象被实例化为默认类(SKNode,SKSpriteNode等),我不知道如何绑定它们以实例化为自定义子类.
目前,我正在通过创建自定义类并链接到sprite节点作为属性来解决这个问题,这样就可以了
目前你应该在添加到SpriteKit游戏模板的方法中按名称进行操作.他们在WWDC 2014的SpriteKit最佳实践视频中介绍了这个主题.很容易错过,因为视频中有很多内容.
这是方法.
+ (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;
}
Run Code Online (Sandbox Code Playgroud)
它是一个类别SKScene,包括NSKeyedUnarchiver从mainBundle中的.sks文件加载时用于替换Class的方法.在iOS中,他们将其添加到GameViewController.m文件中,在OS X中,他们将其添加到AppDelegate.m文件中.
在这里或您的个人SKNode子类中,您可以实现此目的.这就是他在帖子中所做的.这是由Apple提供的,并在WWDC视频中介绍.我想明年它会得到更好的处理.在此期间,文件中的一个错误请求SKNodes获取类似于场景编辑器的IBDesignable.:)
我写了一个小助手委托来处理这个问题:https://github.com/ice3-software/node-archive-delegate。它使用NSKeyedUnarchiverDelegate来按名称对精灵进行子类化。
| 归档时间: |
|
| 查看次数: |
4099 次 |
| 最近记录: |