iosswiftsprite-kitskscenesktransition

SKTransition not working in Swift


I have a very simple SpriteKit game written in Swift with two scenes that I am trying to transition between. This code works perfectly:

let skView = self.view as SKView

let scene = GameScene(size: self.scene.size)
scene.size = skView.bounds.size
scene.scaleMode = .AspectFill

//This line shows the new scene immediately (as expected)
skView.presentScene(scene) 

The trouble comes when I replace the above line of code with this:

let sceneTransition = SKTransition.doorsCloseHorizontalWithDuration(2.0)
skView.presentScene(scene, transition: sceneTransition)

When I do this nothing happens (the current scene remains on screen). I have tried several different transitions, but always with the same result.

Any thoughts?


Solution

  • SOLVED.

    It turns out that there is nothing wrong with the code above. The problem was that I was trying to execute it in the update function based on a specific SKPhysicsBody's position. The transition doesn't happen instantaneously so it gave time for update to get called repeatedly which re-triggered the transition over and over again. This resulted in the transition never happening.

    The fix was to add a bool to check if I have started the transition, so I only attempt it once.