Reputation: 3081
I have built a simple trivia game using a normal Single View type of app using swift. But now, as part of an update, I want to add a more complex trivia level to the game and this level is going to be built using SpriteKit, with animations and whatnot, but I'm not sure how can I transition and show a spriteKit game scene in a simple single view swift app and then transition back to a regular view controller.
I did search all around but I could not find any specific answer to this question, only the other way around.
Any help is highly appreciated.
Upvotes: 4
Views: 1126
Reputation: 48
Not sure if you're still trying to do this, but you can transition between UIKit and SpriteKit scenes pretty easily, doing something like:
let scene = NewTriviaScene(fileNamed: "NewTriviaScene")
scene?.scaleMode = .aspectFill
self.view?.presentScene(scene!, transition: SKTransition.flipHorizontal(withDuration: 0.42)) // or whatever other transition
You would just need to create an .sks scene with the content you want, and set its 'Custom Class' to be a swift code file you also create. That swift code file would import SpriteKit and be an SKScene:
class NewTriviaScene: SKScene { }
Then you could add custom behavior and animations in this code file, and respond to touches and other events. For trivia, guessing you would mainly be concerned with touch events. You can name the nodes in the sks file and reference them in the touches method like:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let loc = t.location(in: self)
if atPoint(loc).name == "someName" { }
Then you could load a new UIKit scene in a similar way as before, inside the touch event or score or whatever.
Upvotes: 2