SpriteKit comes with a modified version of the Box2D physics framework, and it's wrapped up a lot of complicated physics mathematics into just one or two lines of code. For example, we can create a square, red sprite and give it rectangular physics like this:... Continue Reading >>
Pixel-perfect physics is just one line of code in SpriteKit. Don't believe me? Here you go:... Continue Reading >>
You can change between SpriteKit scenes by calling the
presentScene() method on your
SKView. This can be called either just with a new scene, or with a new scene and a transition animation to use, depending on the effect you want. Here's an example with a transition:... Continue Reading >>
One powerful and under-used feature of SpriteKit is its ability to recolor
SKSpriteNodes dynamically. This has almost zero performance impact, which makes it perfect for having multiple-colored enemies or players. It can also be animated, meaning that you could for example make your player flash white briefly when they've been hit by an enemy.... Continue Reading >>
3D audio is a feature where a sound is dynamically altered so that listeners think it comes from a particular location. Obviously they are looking at a flat 2D screen ahead of them, but using some clever mathematics iOS can make sounds "feel" like they are behind you, or at a more basic level adjust the panning so that sounds come from the left or right of the user's audio device.... Continue Reading >>
A SpriteKit texture atlas is actually just a folder with the extension .atlas, but it's more efficient than loading textures individually because multiple images are stored in a single file and thus can be loaded faster. Even better, you don't need to worry about how they are placed or even orientation – you just use them as normal, and SpriteKit does the rest.... Continue Reading >>
SKShapeNode class is a fast and convenient way to draw arbitrary shapes in your games, including circles, lines, rounded rectangles and more. You can assign a fill color, a stroke color and width, plus other drawing options such as whether it should glow – yes, really.... Continue Reading >>
SpriteKit has built-in support for particle systems, which are a realistic and fast way to create effects such as smoke, fire and snow. Even better, Xcode has a built-in visual particle editor so that you can tweak your designs until they look exactly right.... Continue Reading >>
It's just one line of code to find where the user touched the screen when you're using SpritKit, and that one line can even be used to calculative relative positions of a touch compared to any node in your game.... Continue Reading >>
GameplayKit is a powerful new framework introduced in iOS 9.0, and one of the (many!) things it does is provide a number of ways to generate random numbers easily. To get started, import the framework into your code like this:... Continue Reading >>
GameplayKit's random number generator includes help constructors that produces numbers in a specific range, simulating a six-sided die and a 20-sided die. To get started you should import the GameplayKit framework like this:... Continue Reading >>
SpriteKit action groups let you run multiple SpriteKit actions simultaneously. The grouped actions become a new action that can go into a sequence, and SpriteKit automatically ensures all actions in a group finish before the sequence continues.... Continue Reading >>
One of the great features of SpriteKit's actions is that they can be chained together using action sequences. SpriteKit automatically ensures each action finishes before the next one begins – all you need to do is create the actions then put them into an array.... Continue Reading >>
Enabling physics in SpriteKit is just one line of code, but sometimes you want your physics to be a little more nuanced. For example, your player might have circle physics and should respond to gravity, whereas walls might have rectangle physics and not respond to gravity – they are there to be bounced off, but nothing more.... Continue Reading >>
SKLabelNode class is a fast and efficient way to draw text in SpriteKit games. To use it, first create a property in your game scene:... Continue Reading >>
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 4.
Go from iOS to macOS the easy way!
If you like Hacking with Swift, you'll love Hacking with macOS – learn to build macOS apps today, using 18 real-world projects!