just ME
just ME

Reputation: 1827

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

Upvotes: 2

Views: 732

Answers (3)

IronDaddy
IronDaddy

Reputation: 1

  • The first scene is GameScene.
  • The new scene name is NewGameScene.

Code:

override func touchesBegin(touches:NSSet, withEvent event: UIEvent) {
    let reveal = SKTransition.flipHorizontalWithDuration(1.0) 
    // the number is the duration time.  After you type . right behind SKTransition, you can choose other scene transition effect. 
    self.view?presentScene(NewGameScene,transition:reveal)
}

Upvotes: 0

Harout360
Harout360

Reputation: 939

meisenman's answer for Swift 2.0

let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let gameOverScene = GameOverScene(size: self.size)
self.view?.presentScene(gameOverScene, transition: reveal)

Upvotes: 1

meisenman
meisenman

Reputation: 1838

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.

Upvotes: 2

Related Questions