SKScene当前场景转换不适用于Swift中的SpriteKit场景

时间:2016-01-01 15:25:22

标签: ios swift sprite-kit

我正在尝试使SKScene presentScene具有过渡行为。 presentScene在技术上有效,尽管我得到的是默认的MoveUp转换行为。我正在从场景的存档中加载一个SpriteKit场景。我为转换类型添加的内容似乎并不重要,我总是得到默认行为。我已经调试以确保转换具有值(即,不是nil)。我正在使用iCloud和coreData托管对象,虽然我不知道这会如何影响场景呈现。我已将管理对象代码包含在viewWillAppear中。

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)
    }
}

1 个答案:

答案 0 :(得分:3)

你为什么褪色10秒?似乎很久了。这也是GameViewController中的代码吗?

SKTransitions仅在从1 SKScene转换到另一个SKScene时有效。从GameViewController加载第一个场景将不会使用转换,因为从技术上讲没有任何过渡。

作为旁注,在swift 2中你可以摆脱扩展以取消SKScene的归档。你也可以说(如果你使用的是GameScene.sks)

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