Do I need to create a new SpriteKit level editor file for every new .swift file? - ios

I'm trying to get comfortable with Sprite Kit level editor. By default, there is one "gamescene.sks" file that's attached to "gamescene.swift".
If i'm making a "gameoverscene" or "playscene" for example, do I need to create both of them (.swift and .sks) if I want to work with my game in the level editor ?
Also, I'm interested in it's size management. I'm making a universal devices game. Should I change it's default size (1024x768) or do not bother ?
Also that scene canvas looks horizontal, should I change it if my game is a portrait-mode only ?

Short Answer. Yes.
Reason..
Every time you want to present or load an .sks file, You need to load it with a class like so
let doors = SKTransition.doorwayWithDuration(1.0)
let archeryScene = GameScene(fileNamed: "GameScene")
self.view?.presentScene(archeryScene, transition: doors)
If you look a the constant loads the file with a class
MyScene(fileNamed: "MyScene")
So yeah you do need a .swift file for each .sks file.
For more info on .sks files look here.
Techotopia - An iOS 8 Swift Sprite Kit Level Editor Game Tutorial
Also when I use the Spritekit Level Editor, I usually set the Scene size to 960 x 640 to support the smallest iPhone. Alsong as you have #1x, #2x, #3x and possibly #1x~iPad and #2x~iPad, Everything will be fine. Just to be sure in the ViewDidLoad or the initWithSize(CGSize) to add self.scaleMode = .AspectFill. You can set it to what ever it may be. Your options are,
SKSceneScaleMode.Fill
Each axis of the scene is scaled independently so that each axis in the scene exactly maps to the length of that axis in the view.
SKSceneScaleMode.ResizeFill
The scene is not scaled to match the view. Instead, the scene is automatically resized so that its dimensions always matches those of the view.
SKSceneScaleMode.AspectFit
The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible, but may require letterboxing in the view.
SKSceneScaleMode.AspectFill
The scaling factor of each dimension is calculated and the larger of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire area of the view is filled, but may cause parts of the scene to be cropped.
(Credits to #epicbyte - Dealing with different iOS device resolutions in SpriteKit)

Related

GameScene SpriteKit does not fit iPhone X display

I have been looking and trying all solutions I've found on the internet. None of them are working as expected. My game requires a border as shown in the image attached. It works fine with iPhone 8 and 8 Plus. On X, I cannot seem to find the proper way to fill it without disrupting my game scene. Even if I use resizeFill or aspectFill it cuts off the important contents from my gaming area. The one in the image is aspectFit but gives me black borders which does not look good and is not taking advantage of the larger screen. The layout in the GameScene file is not according to iPhone X. How can I make the game optimised for all of these 3 devices. I know how to do it for an app on storyboard with constraints and auto layout, but I am kinda new to SpriteKit and GameScenes. I do not mind even if the black borders on the iPhone X is filled the colour of my border. But I do not know how to implement it and I do not think its the best practice for optimising games for all of these 3 devices. Thank you in advance for your help.
I guess you want something like this right 👆
1. Resizable scene
First of all you want your scene to resize (changing size and aspect ratio) in order to perfectly match the display. So open GameViewController.swift and set
scene.scaleMode = .resizeFill
2. Resizable rectangle
Now you want to create a rectangle matching the perimeter of your scene.
That's easy, open your scene and write what follows
class GameScene: SKScene {
override func didMove(to view: SKView) {
let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)
}
}
How does it work?
As you can see I am creating a rectangle using the same frame of the scene. So, you are sure your rectangle will always match the perimeter of the scene (which will match the display).
That's it.

Properly setting the SKCameraNode's size/scale in a large SKScene

I'm wanting to create a simple game where you fly around a large game scene in SpriteKit, with a camera following the player. It will be a portrait-oriented game.
What I'm wondering is how to properly set up the dimensions of the SKScene and SKCameraNode. I understand that the SKCameraNode's "size" (viewport size?) will match SKScene.size (assuming the scale=1.0).
So am I expected to just make an arbitrarily large scene, say for example square shaped, and then use some weird scaling numbers in the SKCameraNode to make its aspect ratio line up with that of the iPhone?
I ask this because I was following a tutorial that didn't really address this, and the result is that I had a SKScene that ended up being smaller than all of the objects inside of it (to make the camera's aspect ratio correct). Projectiles would just disappear because they'd have exited the bounds of the scene, however that part of the scene (and past it!) would still be in view! Obviously, I know this is wrong, but I want to know what the best way to fix it is.
Thanks very much!

Basic Positioning for Devices in SpriteKit

I'm extremely new to SpriteKit, and have been following some great tutorials by someone online. They all do one thing which I think might be a red flag, but wanted to ask the community about.
Prerequisite: all his demos are done using iPad (it's set in the app settings to only target iPad, so no intention of iPhone).
So what i'm seeing is the developer creates labels and graphics by using hard-coded points.
For example to make the text "game over" appear:
let gameOver = SKSpriteNode(imageNamed: "gameOver")
gameOver.position = CGPoint(x: 512, y: 384)
gameOver.zPosition = 1
addChild(gameOver)
So the thing about this code is it's hard-coded to be at a specific point. So this means that in theory, it's going to be slightly wrong on the iPad mini vs. the iPad pro, correct (since he intended this to be on the iPad air)? Or do the devices translate where that should be every time?
I feel like you'd want to do some fancy magic like calculate the width/height of the screen and divide by some number to calculate the center, rather than specify points.
In fact, all of his code more or less uses hard-coded points for example look at this to render sprites:
for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 410)) }
for i in 0 ..< 4 { drawSpriteAt(CGPoint(x: 180 + (i * 170), y: 320)) }
for i in 0 ..< 5 { drawSpriteAt(CGPoint(x: 100 + (i * 170), y: 230)) }
Is this not a major problem if I were targeting iPad mini and iPad pro as well?
For some reason, however, I ran this on the simulator in iPad pro and it actually looked pretty good.
So my question, ultimately then, is:
Is this just fine to do it this way? Do the different iPads somehow magically work out what the point (512, 384) is such that that point is different on an iPad mini vs. iPad pro?
Thanks and apologies if this is a basic question, I just wanted to see that if I were only writing an app to target iPhones, or iPads (i'm doing both actually) that I would make two separate apps with hard-coded coordinates specific for one device.
For example i'd say "on all iPads, regardless, use point (512,384)" and on an iPhone i'd say "on all iPhones of all sizes, use point (300, 150) or something like that.
Thanks so much for your help!
Sprite Kit is designed to have a scaling system so you don't have to worry as much about coordinates. The coordinates don't represent pixel or point coordinates on the screen - but are coordinates in the game scene. Basically if you setup a scaling mode for you game, your coordinates will be taken care of for multiple screen sizes.
(You would be correct in your assumption if you were just talking about iOS button/view coordinates.)
See the documentation here: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html
After a scene is rendered, its contents are copied into the presenting
view. If the view and the scene are the same size, then the content
can be directly copied into the view. If the two differ, then the
scene is scaled to fit in the view. The scaleMode property determines
how the content is scaled.
When you design your game, you should decide on a strategy for
handling the scene’s size and scaleMode properties. Here are the most
common strategies:
Instantiate the scene with a constant size and never change it.
Pick a scaling mode that lets the view scale the scene’s content. This gives
the scene a predictable coordinate system and frame. You can then base
your art assets and gameplay logic on this coordinate system.
Adjust
the size of the scene in your game. Where necessary, adjust your game
logic and art assets to match the scene’s size.
Set the scaleMode
property to SKSceneScaleModeResizeFill. SpriteKit automatically
resizes the scene so that it always matches the view’s size. Where
necessary, adjust your game logic and art assets to match the scene’s
size.

IOS Swift SpriteKit Scaling Entire Views According to Frame Size

I would like the scenes of my game in SpriteKit to appear differently sized depending on the size of the device. Right now I am manually resizing and positioning some of my nodes with an 'if' statement checking for frame size, but unfortunately I cannot do this for all of my sprites due to the mechanics of my game (I am detecting collisions based off of the positions of my nodes, not by using PhysicsBodies. If I were to change the size of the nodes, these collisions would not be detected).
Is there a way to scale the appearance of the view according to the device size instead of actually scaling the sprites themselves (i.e. the view would stretch out to fit frame size but in reality the sprites would actually be the same size)? Is this something I would achieve by changing the SKSceneScaleModes from .ResizeFill (the one I am currently using)?
I don't believe there is an easy way to fix my problem, nor do I believe that the solution I was looking for exists. This is how I fixed my problem:
First, I changed my game mechanics. I've learned that when you are going about designing and initially planning out your game, you have to really focus on making your game both easily expandable and universal. The way that I had my game first set up was quite limited.
I figured out how I wanted to scale my game and set up my own little system (not really my 'own', its probably a pretty common setup):
var scene = GameScene(size: self.size)
var skView = self.view! as SKView
scene.size = skView.frame.size
var scale = self.frame.size.height / 736
Then I just set the scale to my nodes like so:
self.titleText.setScale(scale)
First, I am setting the size of the current scene as the size of the frame size of the skView. With that, I am creating a scale factor by dividing the size of the scene by the frame size of an iPhone 6 plus. The sprites will now appear at a scale factor of 1 on the iPhone 6 plus and downscale from there.
Anyways, I know this explanation wasn't perfect and that there is probably a more efficient way of doing this. I am relatively new to SpriteKit and have a lot to learn, but I didn't want to leave this question unanswered on the off chance that somebody stops by here. Thanks, and feel free to message me if you have any questions or I messed up somewhere.

Dealing with different iOS device resolutions in SpriteKit

I'm playing around with SpriteKit in Xcode 6, iOS 8 beta 5. Everything is all laid out and working perfectly on the iPhone 4S simulator, however when switching to the 5S, the elements at the bottom of the screen are cut off.
It was to my understanding that the bottom left corner of the iPhone screen should be CGPoint(0, 0) but after checking the location by printing the coordinates to the console that the lowest point of the left corner I could click was around (5, 44). Is there something wrong in my scene setup thats causing this?
No changes have been made to the GameViewController file and even after I strip the GameScene file the problem persists.
Can anyone at least point me in the right direction with this?
Adding the following code will fix your problem (code is in Swift):
scene.scaleMode = SKSceneScaleMode.ResizeFill
Now if you want to know why this fixes your problem, what your problem actually is, and how to handle multiple resolutions – I suggest you continue reading.
There are three things that can impact the position of nodes in your scene.
1) Anchor Point
Make sure your scene's anchor point is set to (0,0) bottom left. By default the scene's anchor point starts at (0,0) so i'm assuming that is not causing the issue.
2) Size Check the size of your scene. I typically make my scene size match the size of the device (i.e. iPad, iPhone 4-inch, iPhone 3.5 inch), then I place another layer in the scene for storing my nodes. This makes me able to do a scrolling effect for devices with smaller resolutions, but it depends on your game of-course. My guess is that your scene size might be set to 320, 480 which could be causing the positioning problems on your iPhone 5s.
3) Scale Mode The scale mode has a huge effect on the positioning of nodes in your scene. Make sure you set the scale mode to something that makes sense for your game. The scale mode kicks in when your scene size does not match the size of the view. So the purpose of the scale mode is to let Sprite Kit know how to deal with this situation. My guess is that you have the scene size set to 320,480 and the scene is being scaled to match the iPhone 5 view which will cause positioning problems identical to what you described. Below are the various scale modes you can set for your scene.
SKSceneScaleMode.AspectFill
The scaling factor of each dimension is calculated and the larger of
the two is chosen. Each axis of the scene is scaled by the same
scaling factor. This guarantees that the entire area of the view is
filled, but may cause parts of the scene to be cropped.
SKSceneScaleMode.AspectFit
The scaling factor of each dimension is calculated and the smaller of
the two is chosen. Each axis of the scene is scaled by the same
scaling factor. This guarantees that the entire scene is visible, but
may require letterboxing in the view.
SKSceneScaleMode.Fill
Each axis of the scene is scaled independently so that each axis in
the scene exactly maps to the length of that axis in the view.
SKSceneScaleMode.ResizeFill
The scene is not scaled to match the view. Instead, the scene is
automatically resized so that its dimensions always matches those of
the view.
Conclusion
It looks like you want to remove the scaling of your scene, that way your positions in the scene will match the actual positions in the view. You can either set your scene's size to match the view size, in which case no scaling will take place. Or you can set your scene's scale mode to ResizeFill which will always make the scene's size match your view's size and it won't scale anything. In general I would stay away from any scaling and instead adjust the interface and the scene size to best suit each device. You may also want to add zoom and/or scrolling to allow devices with smaller resolutions to achieve the same view field.
But what if I want to scale my scene?
If however you need to scale your scene, but you still want positions to be relative to the view (i.e. You want (0,0) to be the bottom left of screen even when scene is cutoff) then see my answer here
Additional Info
See answer here for sample code showing how I layout nodes dynamically.
See answer here for more details about scaling to support multiple devices.
If you want to preserve the size of your scene (usually desired when you work with a fixed size and coordinates system), you might want to add padding to either side of your scene. This would remove the letter boxing and preserve all the physics and dynamics of your app on any platform.
I created a small Framework to help with this:
https://github.com/Tokuriku/tokuriku-framework-stash
Just:
Download the ZIP file for the Repository
Open the "SceneSizer" sub-folder
Drag the SceneSizer.framework "lego block" in your project
Make sure that the Framework in Embedded and not just Linked
Import the Framework somewhere in your code import SceneSizer
And you're done, you can now call the sizer Class with:
SceneSizer.calculateSceneSize(#initialSize: CGSize, desiredWidth: CGFloat, desiredHeight: CGFloat) -> CGSize
Just in case, try doing CMD+1, worked for me. Some of the elements were cut off because they were simply not displayed in Simulator - I stress this, this is just a simulator feature (and a bug if you ask me, wasted hours of time to solve this). CMD+2, CMD+3 views can sometimes hide parts of the scene.

Resources