Search code examples
swiftsprite-kitskspritenodeskscene

swift SKSprite Move from one GameScene to Another


I am new to Swift and SKSPRITE

I added a button on my first class GameScene: SKScene. When the button is cliecked I want to move to my new GameScene. How can I accomplish this?

I have the method override func touchesBegin(touches:NSSet, withEvent event: UIEvent) { ... }


Solution

  • http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

    My answer is stripped from this Ray Wenderlich tutorial:

    SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
    SKScene * myScene = [[YourSceneName alloc] initWithSize:self.size];
    [self.view presentScene:myScene transition: reveal];
    

    This is not swift but should still work with your code.

    YourSceneName should be the name of the scene you want to transition to. Remember to import the header in the class you wish to call it from.

    To make a button, simply create an SKSpriteNode and in touchesBegan or Ended, check if the touches location is inside spriteNode. If it is, execute the code posted above.