How to add a fragment shader to an SKSpriteNode using SKShader
Fragment shaders let you adjust individual pixels inside sprites to create effects such as embossing, pixellation, and even water, and you can attach fragment shader to any SKSpriteNode
just by setting its shader
property.... Continue Reading >>
How to add physics to an SKSpriteNode
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 >>
How to add pixel-perfect physics to an SKSpriteNode
Pixel-perfect physics is just one line of code in SpriteKit. Don't believe me? Here you go:... Continue Reading >>
How to advance time in an SKEmitterNode using advanceSimulationTime()
When you create a particle system in SpriteKit, the particles start at their creation point and move outwards from there. That’s fine for things like explosions and fire, but if you’re using the particles to simulate something that has no real start or end – space dust, for example – then having your particles start from a particular location looks wrong.... Continue Reading >>
How to change a sprite’s texture using SKTexture
Although you can create an SKSpriteNode
from a color and size, most folks create them from textures – image data stored in an asset catalog or texture atlas. SpriteKit’s textures are handled using their own class called SKTexture
, and you can load them individually then use them to change the texture used to draw a sprite.... Continue Reading >>
How to change SKScene with a transition: presentScene()
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 >>
How to color an SKSpriteNode using colorBlendFactor
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 >>
How to create 3D audio sound using SKAudioNode
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 >>
How to create a random terrain tile map using SKTileMapNode and GKPerlinNoiseSource
Tile maps are designed to enable extremely efficient drawing of large amounts of terrain, and work by building grids of smaller images that combine to form large and varied maps. Helpfully, SpriteKit automatically manages the tiles to keep their overhead as low as possible, and even builds a tile map editor directly into Xcode so you can literally paint your maps by clicking around.... Continue Reading >>
How to create a SpriteKit texture atlas in Xcode
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 >>
How to create shapes using SKShapeNode
SpriteKit's 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 >>
How to crop a sprite using SKCropNode
SpriteKit has a dedicated node type for handling cropping, and you can add things to it, position it where you want, then add it to your scene just like any other node.... Continue Reading >>
How to debug physics in a SpriteKit scene using showsPhysics
SpriteKit’s SKView
class has a few built-in debugging options, of which two are enabled by default: showsFPS
to show the current frames per second, and showsNodeCount
to show how many nodes are currently in your scene.... Continue Reading >>
How to emit particles using SKEmitterNode
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 >>
How to find a touch's location in a node using location(in:)
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 >>
How to generate a random number with GKRandomSource
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 >>
How to generate fair random numbers using GKShuffledDistribution
A so-called “fair” random number generator is one that generates each of its possible values in equal amounts and with an even distribution. For example, if you were generating numbers between 1 and 4, you might get 4, 2, 1, 3, but you would never get 4 4 1 4.... Continue Reading >>
How to generate shaped random numbers using GKGaussianDistribution
A shaped random number generator is one that generates each of its possible values but does so in a way that numbers near the middle are more frequent. For example, you might use it generate heights of characters, because most people are around average height while some outliers are much shorter or much taller. For example, if you were generating numbers between 1 and 10, 5 and 6 would be generated significantly more than 1 or 10.... Continue Reading >>
How to made an SKSpriteNode render faster using blendMode
All SpriteKit nodes have a blendMode
property that describes how they should be drawn to the screen. The default value is .alpha
, which means the sprite should be drawn so that its alpha transparency is respected – any parts that are translucent get blended with the existing background color at that point, and any fully transparent parts are not drawn at all.... Continue Reading >>
How to make a sprite follow a path
SpriteKit makes it easy for a node to follow a path we specify, and you can use it to make rockets take off, enemies to zig zag around a maze, and more.... Continue Reading >>
How to make one sprite draw in front of another using zPosition
All nodes in SpriteKit have a zPosition
property that dictates its depth on the screen. If you’re using Xcode’s default SpriteKit template then the view you’re rendering into has its ignoresSiblingOrder
property set to true, which means zPosition
is the only factor that decides whether one node is drawn above or below another.... Continue Reading >>
How to roll a dice using GameplayKit and GKRandomDistribution
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 >>
How to run SKActions in a group
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 >>
How to run SKActions in a sequence
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 >>
How to simulate gravity in a SpriteKit scene
Once you’ve given all your SpriteKit nodes physics bodies, you might want to add some simulated gravity so they fall to the ground over time. This technique is also useful if you want to simulate wind (think of it like horizontal gravity), or even for making the user tilt their device to make nodes fall in different directions.... Continue Reading >>
How to stop an SKPhysicsBody responding to physics using its dynamic property
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 >>
How to warp a sprite using SKWarpGeometryGrid
SpriteKit allows you to warp sprites by dividing them up into small squares then stretching those squares into different positions. The result is that you can warp a sprite in various ways – you can effectively pull different parts of it however you want.... Continue Reading >>
How to write text using SKLabelNode
The 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 >>
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift.
Link copied to your pasteboard.