Switching from a SpriteKit scene to an UIView in Swift - swift

I'm developing a Swift SpriteKit game and I need a little help. I have my game scene where the game runs and when the player dies I want to load another scene with buttons and labels for score, but I want this scene to be an UIView instead of a SpriteKit scene. How can I load the UIView from the SpriteKit scene? Works with present scene? Thanks a lot!

If you just want to show a popup-menu, you can access the view of your SKScene and add your UIView as subview:
self.view?.addSubview(yourView)
Or if you want to move to another UIViewcontroller, you can access the rootViewController of your view and present your viewcontroller from there:
self.view?.window?.rootViewController?.presentViewController(viewControllerToPresent: YourViewController, animated: true, completion: nil)

Related

Add a Sprite Kit View to a UIView Xcode 8.2.1

I am making a app that will hopefully have a game in it but in order to do that I need a Sprite Kit View. I was wondering if there was a way to add a Sprite Kit View to a Single View Application?
Yes, SKView is just a subclass of UIView, so you can add it via code where you want or through the Storyboard. as of XCode 9 I believe, IB allows you to add an actual SKView where you can set the scene file instead of having to fill the custom class field in.

Swift - Sprite kit object in a single view applicationf

I know that it is possible to integrate Sprite kit to a single view application but I want to know I if it is possible to create a object in sprite kit file and then put that on a view? Do I have to set the view class to be SpriteKit class in order to have the object? Thanks!
SKScene is attached to SKView which is just a UIView object. You can even put spritekit content in your SVA via storyboard. Or, you can put UIKit content in you SK app via the SKView property in your GameViewController or, via the self.view property in your GameScene.

SpriteNode behind UIImages

I've created a SpriteNode in a scene and it's there, but it is stuck behind all the UIImages and UIButtons I've dragged onto the storyboard.
I tried changing the .zPosition of the spriteNode to 99999999999999, but it doesn't matter.
What do I need to do to get the spriteNode to appear in front of most of the images? Or can I only have one background image?
Unfortunately for you, this is impossible. You are combining two rendering hierarchies (UIKit and Sprite Kit) whose children (UIView and SKNode, respectively) do not mingle.
SKNodes are rendered inside the SKScene that contains them. The final rendered image is used as the image for the layer of the owning SKView. From there, UIKit (and CoreAnimation) composite the UIViews.
A hierarchy for illustration purposes:
UIView
UIButton
UIButton
SKView (renders an SKScene object)
SKNode
SKLabelNode
SKLabelNode
UIImageview

How to present a view on top of SKScene?

I know there is many question out there on the same topic but still I didn't get any idea or solution to make it work. How do I implement a small view on top of a scene with options as shown in image?
You can do it within your scene using spritekit nodes or you can create a UIView from your viewController and add it the viewControllers view:
something like this:
// set up base view of the game
self.baseView = UIView(frame: self.view.frame)
self.view.addSubview(self.baseView)
// add my skView to my baseView
self.baseView.addSubview(skView)
// add pause button on top or other elements
// hide or show these as needed
self.baseView.addSubview(self.pauseBtn)
self.addJoySticks()
You could just use SpriteKit itself and:
scene?.view?.presentScene(anotherScene, transition: SKTransition.fadeWithDuration(1.0))
edit Just to add, If any Background AVPlayers or nodes are in play, then they're all discarded when transitioning.

SWIFT - Can I have a SKView as a subview of a UIView?

In my storyboard, I have a base UIView, then a UIView containing a square board drawing in a CGRect and buttons and status fields above and below. I tried drawing the playing pieces but animating them became a nightmare so I have rendered them as sprite nodes in an array and they animate themselves. So far so good. Then layer on top of that is a SKView view and I want to present the SpriteKit scene in there with skView.allowsTransparency = true so I just have the sprites over a transparent background (revealing the board below) and using TapGestureRecognizers to effect the sprite animation event handling.
So in IB hierarchy is: VC / UIView / boardView: UIView / skView
Problem is, the Spritekit scene always targets the top level UIView not skView so game pieces are behind the board. Am I missing something simple here?
I'm gonna try placing a child VC where skView is but any assist, if I've missed a trick, would be very much appreciated.
I wish I could see some code, but what you do is create an IBOutlet for your SKView, then have that outlet present the scene, I am currently doing this in an app I am working on, so I know it is doable