We will look into how to make a property observable using a separate class managing the observers. It is an alternative and simple way of observing property changes without using… Read more Making a property observable from outer scope using generic class Observable →
Aim of the tutorial is to get started with ReactiveSwift without any previous setup except having Xcode installed. We will go through how to install it using Carthage package manager,… Read more Getting started with ReactiveSwift →
RawRepresentable is a protocol in Swift standard library and enables converting from a custom type to raw value type and back. In this post we’ll be looking into how to implement RawRepresentable for enumeration containing an associated value. Conforming to RawRepresentable Implementing RawRepresentable requires three steps: firstly, choose RawValue type; secondly, implement initialiser where RawValue type is matched to one of the cases in the enumeration, and thirdly, rawValue getter where enumeration cases are converted into RawValue type. In the example we’ll be looking into enumeration representing scenes: home, levelSelection… Read more RawRepresentable and associated values →
Getting a random number within a range is a very common operation. There are multiple ways of extending Swift language to add support for getting random values within a specified range. After experimenting with different implementations like utility functions, integer and float extensions I have found that the most natural way of doing it is to extend ClosedRange. This will allow to write a very readable code: Random floating point values 1. Get a random value using a max possible range (note that arc4random_uniform excludes the upper bound). 2. Convert… Read more Random float and integer in Swift →
Having a glow behind a sprite can give game a move lively environment. In this blog post I am going to present a way of adding a glow to SKSpriteNode… Read more Adding an animating glow to SKSpriteNode →
Clamping a value is an operation of moving the value to a range of allowed values. It can be achieved by comparing the value with allowed minimum and maximum values. For example I was deforming SKWarpGeometryGrid from the direction of a point outside the grid and needed to constrain angles between grid points and the contact point. Maximum and minimum allowed angles were related to the angle between the contact point and the grid’s center point. The solution I propose extends FloatingPoint and BinaryInteger protocols and gives a very readable… Read more Clamping numbers in Swift →
iOS devices have several screen sizes and aspect ratios. This is something to keep in mind when building a game in SpriteKit because placing a node at the edge of a screen is not straight forward. But first let’s take a quick look on managing scenes. One way for it is to use scene editor in Xcode for setting up all nodes in it. When scene is ready and gets presented in SKView, it is scaled based on the scaleMode property (we are looking into SKSceneScaleMode.aspectFill). Without scaling scenes it… Read more Positioning a node at the edge of a screen →
I was working on an upcoming game in SpriteKit only to discover that adding a simple gradient is not so straight-forward as one would expect. Therefore I created an extension… Read more Drawing gradients in SpriteKit →