using SKScene or SKShapeNode for a game world - ios

I'm following this question's advice on how to create my game world for a scroller game with camera centered on the player. However, the advice says to use an SKShapeNode; I don't know how I feel about that.
Are there any advantages/disadvantages to using either a shape node or an skscene (or another type of object/node)? Is there a node that is considered best practice to use as a world?
Also, is it possible to create an skscene inside an skscene, or is that bad practice/discouraged?
Thank you!

You can't put a scene inside a scene per-say but you could stagger view controllers and place a view controller on top of another and then put the scene on top of that inset view controller.
The way I implement my scroller games is to put the player in the center and then move the background around. (Checking for bounds of course.)
As far as your advantages/disadvantages I'm not sure what you mean by the SKNodes; that's how you should do it if you're working just in SpriteKit. But don't add another SKScene unless you have something that you can't implement in just one SKScene.

Related

Two scenes at the same time in SpriteKit

I need to have a game scene, and on the bottom a permanent game menu for things like inventory, etc. I thought it would be great to have two scenes: one for the actual game and another for the game menu. How can I do this in SpriteKit? Having two scenes at the same time (and each one having its own nodes and such)
Also, if this is not the best way to implement this, please tell me.
No don't do that. Even if possible it is meaningless.
In a such case I use storyboard. Created menu at top of the screen and give the rest of the screen to SKView. By this way you can control your SKScene easily also you can use UIKit for you menu which is more advanced than SpriteKit ui elements.
Also you can use one scene and create menu nodes with SpriteKit utilities.

Scenekit orientation in IOS

I have imported a Collada file into my scene and I am using allowsCameraControl to rotate / pan the object / pan the object
I now need functionality to jump instantly to the front or side view of the object.
Does anybody know how I can get the camera that Scenekit implements in my scene and how to rotate this camera to show the front and back view ?
"Front view" and "side view" are notions that are defined in terms of your content, so you have to define them yourself.
Create a node (let's call it "frontViewNode" for example) with an attached SCNCamera and position/orient it so that it's looking at whatever you call the "front" of your object. See this answer if you need help with that.
Create another node ("sideViewNode") that's looking at whatever you call the "side" of your object.
When you want to switch views, set the pointOfView property on the SCNView displaying your scene to either your "frontViewNode" or your "sideViewNode". You can even do this inside an animation (see SCNTransaction) to make the camera view swoop around between the two viewpoints.

Adding a SKSpriteNode to UIView

I'm stuck on something, maybe someone can help me.
I'm working on a iOS swift game (I'm a beginner iOS developer).
In the StoryBoard I've added some buttons, some views, etc.
I've added a reference (of a rectangle) in my controller like (using ctrl+drag):
#IBOutlet weak var mainBoardGame: UIView!
In my GameScene I'm creating some SKSpriteNodes like:
playerSprite = SKSpriteNode(imageNamed: "playerImg")
playerSprite.position = ....
playerSprite.size = CGSize(width:...,height: ...)
self.addChild(playerSprite)
My problem is that this SKSpriteNodes are displayed behind the rectangle (mainBoardGame) created in my storyboard.
I've tried to add something like
playerSprite.zPosition = 100
but still the same.
I'm thinking that it would be better to add my playerSprite directly to my mainBoardGame (because it will be displayed inside of it) but I don't know how to handle a UIView and a SKSpriteNodes.
How can we add a SKSpriteNodes to a UIView ?
How to handle the z order between them ?
The main question is how to handle a UIView and a SKSpriteNode ?
Thanks.
The reason why I've build it in the storyboard is because I put some contraints in order to have a well displayed elements on each of the iPhone screen size.
Is there a way to do that in SpriteKit ?
C.C.
SpriteKit does not play nicely with subviews. SKScene wants to occupy a view entirely, so the high-performance 2D game engine can work its magic.
Why not just create your game board within your scene, and have the enclosing view occupy constraints? It's harder to provide a more specific answer without more detailed code examples.

How to share Background SpriteNode among Multiple Scenes?

Well, the title says it all. If I am currently in a presented Scene of which I have initialized a Background SpriteNode to be a class constant, then how do I use the same Background SpriteNode in my next presented scene without having to create another SpriteNode that behaves and looks exactly the same as in the previous scene.
Will creating another SpriteNode be costly, assuming the background object loads its texture from a texture atlas?
And, I am using Swift.
Something like this should work.
backgroundSprite.removeFromParent()
nextScene.addChild(backgroundSprite)

Sprite Kit: Game menu prefrences. Using a separate scene for the menu, an offscreen node or UIView

Just wanted your opinions on this. I am leaning towards UIView to hold my main menu and such, and just displaying it over the SKView when appropriate. Or I can have an offscreen node with the menu, or I can make a whole new SKScene to transition to.
Do you have any other alternatives or advice on the topic?
If we want to be specific about the scope of the game. It will be something along the lines of an infinite runner. So the menu would be pretty simple (Start, leaderboard... etc)
I'm also developing a SpriteKit game and also needed to display a menu with options and a settings menu, in my case, I found it better to use UiView to present most of the settings of the game as well as a tutorial view because it is a UipageViewController (to present images for how the game works) but here is the downside of either option:
If you choose to use views for menus and pause the game then the rootController must present it and any buttons, labels, images, etc must be presented and then removed, otherwise they will still sit on top of content. This works but animating stuff is difficult, unlike with another Skcene where elements like buttons can bounce, move, etc
The downside of using Skscene is that simple Ui-elements, like those I mentioned before, are now difficult to work with. For example instead of having a button you now have a node and you must therefore work with the touches methods: began, moving, and ending to make the node behave as a touch-up-inside button. Same with labels, and everything will have to be done programmatically whereas if you want to use views, you could make them in he storyboard and instantiate them as needed.
Conclusion: In my game I only chose to use Skscene for the game itself, (i.e playing) and used the views for pause, settings, about, tutorial. Hope this gives you some idea!
I initially started out with all these non-gameplay scenes as UIViews/UIViewControllers, but later on switched to having everything as an SKScene except for the welcome scene viewcontroller.
It doesn't seem very difficult having the options/settings/high scores type of screens under the SKScene option. You really only need to use one method to detect the UITouch:
touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
This works great with sklabels. You could easily load a spritekitnode behind the sklabel and get the same full effects of the UIButton with this combination. You can of course all all touch methods, but touchesBegan almost mimics the expected behavior.
With these views under an SKScene, you also get access to the update: method where you can get more creative in how you display animations and events.

Resources