2015-09-12 64 views
0

我已到處尋找關於如何從.SKS文件初始化視圖的示例。我發現的每個例子似乎現在都會隨着Swift 2和Xcode 7的推出而中斷。我正在使用Xcode 7 GM。Xcode 7 Swift 2 - 如何使用.SKS文件初始化視圖?

有人請提供一個如何以最快的方式做到這一點的例子嗎?

編輯*在將此標記爲重複項之前,請注意iOS版和Xcode版。

+0

可能重複(http://stackoverflow.com/questions/24212771/using-sks-file-with-skscene-subclass) – cybermonkey

+0

@cybermonkey請看之前的版本你標記爲重複的問題。另外,請注意一個人使用Objective-C,這是Swift。 – VERNSTOKED

+0

'view'是指'SKScene'還是'SKScene'子類?如果是這樣,建議您在創建新項目時選擇「遊戲」模板時,查看由Xcode創建的默認項目。 – ABakerSmith

回答

1

我從SpriteKit示例項目中複製了我的代碼。我現在在工作,如果你需要我,我可以在本週末把它轉換成Swift。運行在iOS 9.1上的Xcode 7中。 [使用SKS文件,SKScene子類]的

@implementation SKScene (Archiving) 

+ (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 

// ... 

@implementation VTMillScene 

+ (instancetype)loadMill { 
    return [self unarchiveFromFile:@"mill"]; // loads "mill.sks" 
} 

@end 
相關問題