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



我正在尝试让一个SKScene呈现具有转换行为的场景工作。目前的场景在技术上是可行的,尽管我得到的只是默认的MoveUp转换行为。我正在为场景从存档加载SpriteKit场景。我为转换类型输入了什么似乎并不重要,我总是得到默认的行为。我已经进行了调试,以确保转换有一个值(即,不是nil)。我正在使用iCloud和coreData管理的对象,尽管我不知道这会对场景演示产生什么影响。我已经在视图WillAppear中包含了托管对象代码。

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

您为什么要衰落10秒?看起来很长。这也是你的GameViewController中的代码吗?

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

顺便说一句,在swift 2中,你可以去掉扩展来取消对SKScene的调用。你也可以说(如果你使用的是GameScene.sks)

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

最新更新