SKTransition not working in Swift

1k views Asked by At

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?

2

There are 2 answers

0
Scooter On BEST ANSWER

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.

0
bshirley On

If anyone else experiences this, you might also want to pause the outgoing scene. Some of the logic in it might be changing the state or interfering with the transition.

try setting

  transition.pausesOutgoingScene = YES;

to see if that help,

either use that as a solution, if that's your intent, or set the breakpoints (try update: after the transition is created) and see what's up.