NEW: Pre-order my Vapor book and learn server-side Swift! >>

Games

Found 15 articles in the Swift Knowledge Base for this category:

 

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 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 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 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 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 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 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 >>

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!

Click here to visit the Hacking with Swift store >>