Swift - Sprite kit object in a single view applicationf - swift

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.

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 - 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

Switching from a SpriteKit scene to an UIView in 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)

How to place sprite kit elements overtop of regular UIKit elements

I'm trying to geive the user the ability to drag a skspritenode over a UIImageView and UIButton, but whenever I create the scene it appears behind the UI elements. How do I fix this?
I am afraid that is just not possible. All the sprites and any other elements are rendered frame by frame on the SKScene node and cannot be "dragged" or placed anywhere else. These are not UIKit elements.
In this case, your view is acting as the SKScene and hence all the sprites are being rendered onto that.
SKView is a UIView/NSView in itself. All the Sprite Kit nodes (the scene is also a node) are constituents of the SKView and contained within it. You can only change the draw order of views but not their contents.
To give an example: what you're trying to do is the equivalent of trying to place a UIImageView on top of a UIButton's background but below the UIButton's label.
While technically you could achieve this for plain UIView elements with some trickery, the same will never work for Sprite Kit nodes. They aren't views to begin with but drawn onto an OpenGL framebuffer represented/managed by the SKView. From the perspective of Cocoa an SKView is a single view with no subviews in it.
Simply put: nodes are not views, and nodes are technically incompatible to views.
The UIImageView and its UIButton and whatever other content you want in there must be in a UIView that's sitting lower than the SKView you have the Sprite Kit contents in.
And you'll need to ensure the SKView is transparent (at least its background) and that it passes touches through to the UIView underneath if you need them down there sometimes.

Is it possible to add a SpriteKit scene with transparent BG on top of a uikit view hierarchy?

I'd like to take advantage of the particle effects (and maybe some other stuff) in SpriteKit but my app is mainly rooted in UIKit. Does the SpriteKit framework allow you to create a scene with a transparent background that I can place on top of my UIKit view hierarchy?
Short answer is no.
You can add SKView (subclass of UIView) to your view hierarchy, and set its scene to your own SKScene. But SKView has a backgroundcolor property that cannot be clear (defaults to grey).
It seems possible in OSX mavericks, so hopefully apple will add this possibility in iOS as well.
If your app is mainly rooted in UIKit, Sprite Kit probably isn't the best way to get spiffy visual effects. Some alternatives:
Core Animation provides a particle system in the CAEmitterLayer class.
UIKit has dynamic, physics-based behaviors for animating UI elements.
As of iOS 8, Apple has added this ability: see SKView.allowsTransparency.
https://developer.apple.com/documentation/spritekit/skview/1519697-allowstransparency
It defaults to false, yielding the black rectangle background you see; but set it to true, set its background color to .clear, and it composites over UIKit content just fine.
Further info: Removing Background of SKView - Particle Emitter - SpriteKit