2016-01-01 62 views
0

我正在嘗試獲取帶有轉換行爲的SKScene presentScene。雖然我所得到的是默認的MoveUp轉換行爲,但本技術可行。我從場景的存檔加載SpriteKit場景。它似乎並不重要,我爲轉換類型放置什麼,我總是得到默認行爲。我已經調試過,以確保轉換有一個值(即不是零)。我正在使用iCloud和coreData管理對象,但我不知道這將如何影響場景演示。我在viewWillAppear中包含了託管對象代碼。SKScene當前場景轉換不能在Spift中使用SpriteKit場景

class GameViewController: UIViewController, GameDelegate { 
var managedObjectContext: NSManagedObjectContext! 
var heroCharacter : HeroCharacter! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    presentGameScene() 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    // access the application managed object context - jwg 
    managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 

    // Persistant Store Changes uses the coordinator 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivePersistantStoreDidChange", name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receivePersistantStoreWillChange:", name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: managedObjectContext.persistentStoreCoordinator) 

    // iCloud notifications using the coordinator 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveICloudChanges:", name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: managedObjectContext.persistentStoreCoordinator) 
} 

// MARK: scenes 
func presentGameScene() { 
    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { 

     // Configure the view. 
     let skView = self.view as! SKView 
     skView.showsFPS = true 
     skView.showsNodeCount = true 

     /* Sprite Kit applies additional optimizations to improve rendering performance */ 
     skView.ignoresSiblingOrder = true 

     /* Set the scale mode to scale to fit the window */ 
     scene.scaleMode = .AspectFill 

     // set target values 
     scene.heroCharacter = heroCharacter 
     scene.viewDelegate = self 

     // this transition is not working 
     let transition = SKTransition.fadeWithDuration(10) 
     skView.presentScene(scene, transition: transition) 
    } 
} 

現場代碼以及

override func didMoveToView(view: SKView) { 
    // access the application managed object context - jwg 
    managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 

    // Setup physics world's contact delegate 
    physicsWorld.contactDelegate = self 

    // Setup waterfall 
    waterfall = self.childNodeWithName(kWaterfallName) as? SKSpriteNode 

    // Setup goal 
    goal = self.childNodeWithName("goal") as? SKSpriteNode 

    // Setup initial camera position 
    updateCamera() 

    // load texture atlases 
    CharacterSprite.loadTextureAtlasArrays() 

    var textureAtlasArray = [SKTextureAtlas]() 
    textureAtlasArray.append(SKTextureAtlas(named: kIcebergAtlas)) 

    SKTextureAtlas.preloadTextureAtlases(textureAtlasArray, withCompletionHandler: {() -> Void in 
     #if DEBUG 
      print("completed loading atlases") 
     #endif 
     self.startScene() 
    }) 
} 

func updateCamera() { 
    if let camera = camera { 
     camera.position = CGPoint(x: characterSprite!.position.x, y: characterSprite!.position.y) 
    } 
} 

回答

3

爲什麼你10秒的衰落相當通用的?似乎很長。這也是你的GameViewController中的代碼?

SKTransitions只有在從1個SKScene轉換到另一個SKScene時纔有效。從GameViewController加載第一個場景不會使用過渡,因爲技術上沒有任何變化。

作爲一個方面說明,在swift 2中,您可以擺脫擴展來取消存檔SKScene。你也可以說(如果你使用GameScene.sks)

if let scene = GameScene(fileNamed: "GameScene") { 
... 
} 
+0

更好地回答第1個問題,然後添加幫助代碼。除了這是正確的,你從一開始就沒有從任何東西過渡,你的場景就在那裏。如果你需要'SKView'在開始時進行轉換,那麼你需要查看'UIView'轉換 – Knight0fDragon

+0

歡呼聲以進行編輯。請記住下一次 – crashoverride777

+0

謝謝。你是正確的,因爲我試圖從視圖控制器呈現場景時進行轉換。所以,嘗試從ViewController運行轉換似乎是問題所在。作爲一個方面說明,如果轉換髮生得比我能看到的要快,我有10秒的時間。正如你所指出的那樣,從ViewController轉換並不相關,因此轉換時間也不是這樣。 – Jacksonsox