Reputation: 1827
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
Reputation: 1
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
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
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