SceneKit and SpriteKit together
I am not answering your two questions directly, instead I will deal with your larger issue which is that you want a HUD on your game. A user interface. I never made a Scenekit
game, but I did make a Spritekit
game and the HUD is a clever trick. Let me explain.
Imagine you are standing behind a camera and looking through to see what the camera is seeing through its lens. You see the scene in front of it. Now take a piece of paper, cut out a rectangular box in the middle and draw on the remaining periphery. Tape this piece of paper to the front of the camera and look through. You still see the scene (a little smaller now), but now you also see the paper with the drawings. This is how HUDs work for the game.
I did some searching and both SpriteKit
and SceneKit
have a camera
. In sceneKit you create a camera node
and set the point of view of the scene to that camera. This represents the player looking out into the scene.
I am not sure if it works the same way in SceneKit
, but in SpriteKit
you can add nodes to the camera that will be locked in place. These become your HUD elements (buttons, score, etc). Since these are all child nodes of the camera
they move when you move the camera so it feels like they are a static HUD. This is how you achieve a HUD. Read up on the documentation for SceneKit
cameras as this will help you understand how to achieve the same effect.
I once also thought about doing it the way you are going now and it proved to be a very painful experience to mix SceneKit
and SpriteKit
so I would caution against it.
Also, while you can mix Interface Builder and SpriteKit
I would caution against it. It basically ruined my first game into a buggy mess. If you do decide to use Interface Builder, try to limit it to menu screens and options screens. But, I found that programmatically creating everything gives you a much smoother, cleaner, and bug free experience.
Hope this helps!
Is there a way to include a SpriteKit scene in a SceneKit Scene?
Yes there is!
You can assign a Sprite Kit scene (SKScene
) as the contents
of a material property (SCNMaterialProperty
) in Scene Kit.
Reusing a single SKView to generate multiple SKTextures for use in SceneKit
Option a) have a lot of advantages (Not for memory), but you can manipulate objects inside material for creating cool effects. (Like a clock)
If you really want to get SKTexture from reuse Scene after, you can use this syntaxis:
planeMaterial.diffuse.contents = SKView().texture(from: skScene)!
I create this example with 2 solutions for the question, please take a look:
https://github.com/Maetschl/SceneKitExamples/tree/master/SKTextureOverPlanes
SpriteKit and SceneKit – How to completely pause a game?
You can't add the SKLabelNode (or anything else) to your scene while the SKView is paused. You will need to return to the run loop so your text is added before pausing the game. Here's one way to do that:
// Add pause text or button to scene
addChild(pauseText)
let pauseAction = SKAction.run {
self.view?.isPaused = true
}
self.run(pauseAction)
SpriteKit/Scenekit TouchesBegan or GestureRecognizer
I've wonder the same thing too - it gets even more interesting when you mix SceneKit and SpriteKit together. I haven't seen any official documentation suggesting one or the other, as they are really for slightly different things. You'll find the Xcode game templates use one or the other (or both, in the case of a cross platform spritekit game).
I think the reason most of the tutorials and blogs use the GestureRecognizer approach is that it does a lot of the work for you, but when you get into more complicated use cases, you may find you need to handle the touches manually and handle gestures yourself (theres a few examples floating around the internet) as I had to for a particular project.
I have also read on a few blogs, that sometimes using the touches approach AND GestureRecognizers together can give incorrect results (specifically missing touches), but that could be stale information - it's worth checking though if you did decided to use both.
So to answer the question, I don't believe there is an official best practice for this, as both are valid and current methods. I'd say use whichever you think fits better and makes the code as simple and clean as possible.
Does SpriteKit or SceneKit support multiplayer?
Yes it is possible.
But multiplayer doesn't depend on a framework like SpriteKit or SceneKit. You can implement multi-user capabilities in games like you would do it for other things like a chat or something like that.
Simple multi-user frameworks, that apple provides are either the MultipeerConnectivity
framework for local multiplayer or, especially for games, GameKit
for multiplayer-games over the internet.
There are many tutorials. For GameKit you can check the tutorial on raywenderlich.com. For the MultipeerConnectivity-Framework I'd recommend you to read the documentation.
Related Topics
Swift Running Code in Periodically Background
How to Calculate a Distance Between Two Anchorentities
How to Drag a Working Slider Using Swiftui
Return Object for a Method Inside Completion Block
How to Observe Torchlevel in Swift
How to Name File Stored to Files App via Uactivityviewcontroller
Swift Execute Command Line Command in Sandbox Mode
How to Customise UIsearchcontroller
Expose an Interface of a Class Loaded from a Framework at Runtime
Swift Protocol That Is Using an Enum with Generic Associated Type
Sprite Kit Game Crashes on Game Over on Tvos 9.1 and iOS 9.2
How to Disabled Some Default Functionality in Scene View When Allowscameracontrol = True
Calling Asynchronous Method Inside For-Loop
Generating a Simple Algebraic Expression in Swift
Can't Unwrap Optional.None When Setting Window Background Color