-1

I want to keep a pointer to my GameViewController in a scene in order to be able to transition between scenes from it. I come from C++ and this is a topic I'm struggling to understand in swift. How can this be done?

This is what I'm trying:

class SplashScene: SKScene
{
    var view = SKView()

    override func didMoveToView(view: SKView) {
        self.view = &view
        NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("transitionToGameScene"), userInfo: nil, repeats: false)
    }

    func transitionToGameScene() {
        let scene = GameScene()
        view.ignoresSiblingOrder = true
        view.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1))
    }
}
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

1 Answers1

1

SKScene already has a view property that is its containing SKView so, for that part of your question, just use the already existing property:

class SplashScene: SKScene
{
    override func didMoveToView(view: SKView) {
        NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("transitionToGameScene"), userInfo: nil, repeats: false)
    }

    func transitionToGameScene() {
        let scene = GameScene()
        view.ignoresSiblingOrder = true
        view.presentScene(scene, transition: SKTransition.crossFadeWithDuration(1))
    }
}

As for your question about pointers: Swift doesn't really have them, at least not pointers to memory locations the ones in c++. You can do what your original code was trying to do, just don't use the & operator; Swift will store a reference to your object instance internally (similar to a c++ pointer, but not something you can mess with directly).

You do have to be careful when it comes to structs vs classes though. Swift will store references to class instances, but with structs it will actually make a copy of the entire data structure instead of storing a reference to it. See "Structures and Enumerations Are Value Types", and "Classes Are Reference Types" in the Swift language reference.

Also the & operator is used from time to time in Swift. It's main use is with In-Out Parameters. You can think of it in pretty much the same way that you do in c++ in that case.

Mike S
  • 41,895
  • 11
  • 89
  • 84
  • hi Mike, I could make it work with that code, however my game becomes laggy. I cannot understand why, as the framerate and memory usage don't seem to change. Do you know what might be the issue? – lisovaccaro Aug 24 '14 at 03:34
  • @lisovaccaro I actually don't have a whole lot of experience with SpriteKit's transitions, so your guess is probably as good as mine. If it's just laggy during the transition, then my only thought offhand is to try pausing your scene before the transition. In your scene you can pause with `view.paused = true` (you can pause any individual `SKNode` and its children by setting its `paused` property to `true`). – Mike S Aug 24 '14 at 03:51
  • there aren't problems during the transition, but when I get to GameScene from the SplashScene the character movement becomes laggy. I'd swear the framerate is dropping if it didn't show 60fps. – lisovaccaro Aug 24 '14 at 03:57
  • @lisovaccaro weird... did you try pausing `SplashScene` to see if that helps? Maybe it's trying to run both scenes even after the transition for some reason? I'm assuming that if you take out `SplashScene` altogether then everything works fine? – Mike S Aug 24 '14 at 04:01
  • yes, I suspected the same thing, I'd added a println to the update function inside SplashScene but it isn't running, I just tried pausing it too. It could be a bug, the code doesn't leave a lot of space for a mistake http://stackoverflow.com/questions/25467614/show-a-splashscreenscene-before-to-gamescene – lisovaccaro Aug 24 '14 at 04:16