• Stars
    star
    661
  • Rank 68,192 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sprite Kit helper classes and functions. From the book iOS Games by Tutorials.

Sprite Kit Utils

A collection of Sprite Kit helper classes and functions, written in Swift.

This code was originally written for the book iOS Games by Tutorials, Second Edition, which is published through raywenderlich.com.

iOS Games by Tutorials

SKTUtils requires Xcode 6.1. For the older Objective-C version of SKTUtils, see the objective-c branch.

What can SKTUtils do for you?

It defines handy constants such as ฯ€.

It extends CGPoint and CGVector so you can do:

let pt1 = CGPoint(x: 10, y: 20)
let pt2 = CGPoint(x: -5, y: 0)
let pt3 = pt1 + pt2
let pt4 = pt3 * 100
println("Point has length \(pt4.length())")
let pt5 = pt4.normalized()
let dist = pt1.distanceTo(pt2)

It adds handy functions to Int and Float:

let x = 100
let y = x.clamped(10...50)
let z = Int.random(20..30)

let r = 180.degreesToRadians()
let d = ฯ€.radiansToDegrees()

It extends various Sprite Kit classes with convenience methods, such as:

let color = SKColor(red: 255, green: 128, blue: 64)
let action = SKAction.afterDelay(2.0, runBlock: { /* your code here */ })

And much more... including SKTEffects, which lets you make your games much more juicy!

Introducting SKTEffects

Sprite Kit has a handy feature named actions that make it really easy to move, rotate and scale your sprites. However, a big downside is the omission of timing functions beyond the standard ease in and ease out. The SKTEffects classes from this package add support for many more easing functions to Sprite Kit.

Note: The iOS 8 version of Sprite Kit includes an SKAction.timingFunction property, but unfortunately it is pretty useless. It's a step in the right direction, but it still won't let you perform the kinds of effects that make games juicy.

It lets you do things like this with just a few lines of code:

The demo app

The only reason SKTEffects exists is because SKAction does not allow arbitrary timing functions, only standard ease-in and ease-out. The SKTEffect subclasses are re-implementations of what SKAction already does but with the addition of custom timing functions. It's a bit of a roundabout way of achieving something that really should have been built into Sprite Kit.

There are currently three SKTEffect subclasses:

  • SKTMoveEffect
  • SKTRotateEffect
  • SKTScaleEffect

You use them like this:

let moveEffect = SKTMoveEffect(node: node, duration: 1.0, startPosition: startPoint, endPosition: endPoint)

moveEffect.timingFunction = SKTTimingFunctionBounceEaseOut

node.runAction(SKAction.actionWithEffect(moveEffect))

First you create the SKTMoveEffect object and pass it the node that it should animate, the duration of the animation in seconds, and the starting and ending position of the node.

Then you (optionally) set the timing function on the effect object. You can use the supplied timing functions -- for example, elastic, bounce, and many others -- or create your own. See SKTTimingFunctions.swift for a complete list.

Finally, you wrap the effect object inside a regular SKAction and run that action on the node.

The process for SKTRotateEffect and SKTScaleEffect is identical, but you specify rotation angles and scale vectors, respectively.

You can combine multiple effects at the same time, e.g. have more than one scale effect going at once on the same node.

Warning about SKTScaleEffect

IMPORTANT: When using SKTScaleEffect, the node that you're scaling must not have a physics body, otherwise the physics body gets scaled as well and collision detection becomes unpredictable (objects may suddenly move through other objects).

To solve this, make a new SKNode, give it the physics body, and add the node that you're scaling as a child node.

Caveats

Currently there is no "relative" version of the effects. You always have to supply an absolute starting and ending position, rotation angle, or scale. Most of the time this is no big deal, but it does mean you cannot put them into repeating actions.

For example, the demo project does the following to rotate a node every second by 45 degrees:

node.runAction(SKAction.repeatActionForever(SKAction.sequence([
  SKAction.waitForDuration(0.75),
  SKAction.runBlock {
    let effect = SKTRotateEffect(node: node, duration: 0.25, startAngle: node.zRotation, endAngle: node.zRotation + ฯ€/4)
    
    effect.timingFunction = SKTTimingFunctionBackEaseInOut
    
    node.runAction(SKAction.actionWithEffect(effect))
  }])))

If the effects had a relative version, this could have simply been written as:

let effect = SKTRotateEffect(node: node, duration: 0.25, byAngle: ฯ€/4)

effect.timingFunction = SKTTimingFunctionBackEaseInOut

node.runAction(SKAction.repeatActionForever(SKAction.sequence([
  SKAction.waitForDuration(0.75),
  SKAction.actionWithEffect(effect)
  ])))

Not only is this simpler to read, it also saves you from having to create a new effect instance for every repetition. However, this doesn't work in the current version of the library.

Effects keep state (unlike SKActions), so you should not reuse the same effect instance in multiple actions.

If you use a lot of effects over a long period of time, you may run into memory fragmentation problems, because you need to allocate a new object for every effect. Currently, effects cannot be reset, so it's tricky to put them into an object pool and reuse them.

Because actions keep state, you cannot put them into an action after a delay if the node also moves in the mean time. In other words, doing the following may or may not work:

let effect = SKTMoveEffect()

let action = SKAction.sequence([
  SKAction.waitForDuration(5.0),
  SKAction.actionWithEffect(effect)
  ])

If the node has moved during the delay, either through another SKAction, physics, or the app changing the node's position property, then the effect will start in the wrong place.

Let's get SKTEffects included in Sprite Kit!

If you think custom timing functions are an important feature to have built into Sprite Kit, then go to bugreport.apple.com and duplicate this feature request. The more they receive, the better!

The demo app

The Examples/Effects folder contains a little demo project that shows how to do animations with more interesting timing functions. This app uses physics to move the balls and detect collisions.

It has the following special effects:

  • The objects appear with an animation when the game starts
  • Screen shake on collisions
  • Screen rotate on collisions, for extra shaky goodness!
  • Screen zoom on collisions
  • Color glitch (flashing background color)
  • Ball scales up on collisions
  • Ball smoothly rotates in the direction it is flying
  • "Jelly" effect on the obstacles on collisions
  • And more...

Most of these effects are cumulative; i.e. if there are several collisions in quick succession, then the screen shake movement is the sum of these hits.

All the fun happens in MyScene.swift. There are several let statements at the top that let you turn effects on or off.

Tap the screen to add a random impulse to the balls.

Playground

The Examples/Playground folder contains an Xcode workspace with a Playground. To use this,

  1. Open SKTUtils.xcworkspace in Xcode.
  2. Press Command+B to build the SKTUtils module -- this is important!
  3. Open MyPlayground.playground and start messing around.
  4. Press Option+Command+Enter to open the Assistant Editor so you can see the output.

Have fun playing with SKTUtils!

Unit tests

The Examples/Tests folder contains an Xcode project with unit tests for SKTUtils. Press Command+U to run the tests.

More Repositories

1

swift-algorithm-club

Algorithms and data structures in Swift, with explanations!
Swift
28,779
star
2

swift-style-guide

The official Swift style guide for Kodeco.
12,820
star
3

objective-c-style-guide

A style guide that outlines the coding conventions for Kodeco
3,092
star
4

flta-materials

The projects and the materials that accompany the Flutter Apprentice book
Dart
2,344
star
5

c-sharp-style-guide

C# Style Guide for Game Tech tutorials
484
star
6

ios-interview

The resources and instructions for the iOS Sample Project interview question.
429
star
7

kotlin-style-guide

Kodeco's Kotlin style guide
387
star
8

met-materials

The projects and the materials that accompany the Metal by Tutorials book.
Swift
318
star
9

comb-materials

The projects and the materials that accompany the Combine: Asynchronous Programming With Swift Book
Swift
290
star
10

deprecated-books

Kodeco Deprecated Books
258
star
11

sui-materials

The projects and the materials that accompany the SwiftUI by Tutorials book
Swift
215
star
12

adva-materials

The projects and materials that accompany the Real-World Android by Tutorials book
Kotlin
213
star
13

mcon-materials

The projects and materials that accompany the Modern Concurrency in Swift book
Swift
207
star
14

mvc-modern-approach

Sample project for an article at raywenderlich.com.
Swift
170
star
15

english-style-guide

Style guide for writing in English for tutorials and articles at Kodeco.
157
star
16

alg-materials

The projects and the materials that accompany the Data Structures & Algorithms in Swift book
Swift
152
star
17

vapor-til

The TIL Application for the Vapor book
Swift
147
star
18

rxs-materials

The projects and the materials that accompany the RxSwift: Reactive Programming with Swift Book
Swift
147
star
19

java-style-guide

The official Java style guide for raywenderlich.com
Shell
146
star
20

da-materials

The projects and the materials that accompany the Dart Apprentice book
Dart
139
star
21

dsk-materials

The projects and the materials that accompany the Data Structures & Algorithms in Kotlin book
Kotlin
137
star
22

jet-materials

The projects and the materials that accompany the Jetpack Compose book
Kotlin
134
star
23

ia-materials

The projects and materials that accompany the UIKit Apprentice book
HTML
131
star
24

sa-materials

The projects and the materials that accompany the Swift Apprentice book.
Swift
124
star
25

dsad-materials

The projects and the materials that accompany the Data Structures & Algorithms in Dart book
Dart
121
star
26

vpr-materials

The projects and materials that accompany the Server-Side Swift with Vapor book
Swift
110
star
27

arch-materials

The projects and the materials that accompany the Advanced iOS App Architecture book
Swift
97
star
28

atdd-materials

The projects and the materials that accompany the Android Test-Driven Development by Tutorials book
Kotlin
95
star
29

rwf-materials

The projects and materials that accompany the Real-World Flutter by Tutorials book
Dart
93
star
30

learning-roadmaps

Learning roadmaps for students at raywenderlich.com.
82
star
31

apr-materials

The projects and materials that accompany the Apple Augmented Reality by Tutorials book
Swift
81
star
32

suia-materials

The projects and the materials that accompany the SwiftUI Apprentice book
Swift
80
star
33

itdd-materials

The projects and the materials that accompany the iOS Test-Driven Development by Tutorials book
Swift
72
star
34

rwi-materials

The projects and materials that accompany the Real-World iOS by Tutorials book.
Swift
69
star
35

ka-materials

The projects and the materials that accompany the Kotlin Apprentice book.
Kotlin
65
star
36

ana-materials

The projects and materials that accompany the Advanced Android App Architecture book
Kotlin
60
star
37

kco-materials

The projects and materials that accompany the Kotlin Coroutines by Tutorials book
Shell
59
star
38

aa-materials

The projects and the materials that accompany the Android Apprentice book.
Kotlin
59
star
39

mad-materials

The projects and materials that accompany the App Design Apprentice book
Shell
58
star
40

des-materials

The projects and materials that accompany the Design Patterns by Tutorials book
Shell
58
star
41

swiftui-example-app-koober

Porting the example app from our Advanced iOS App Architecture book from UIKit to SwiftUI.
Swift
57
star
42

cdt-materials

The projects and the materials that accompany the Core Data by Tutorials book
Swift
51
star
43

video-yfsa1-materials

Student Materials for Your First iOS and SwiftUI App: An App from Scratch.
Swift
49
star
44

kmpf-materials

The projects and materials that accompany the Kotlin Multiplatform by Tutorials book
Kotlin
48
star
45

iat-materials

The projects and the materials that accompany the iOS Animations by Tutorials book
Shell
44
star
46

dbg-materials

The projects and the materials that accompany the Advanced Apple Debugging & Reverse Engineering book
C
43
star
47

mos-materials

The projects and materials that accompany the macOS by Tutorials book
Swift
42
star
48

advs-materials

The projects and materials that accompany the Expert Swift book
Swift
41
star
49

daf-materials

The projects and the materials that accompany the Dart Apprentice: Fundamentals book
Dart
36
star
50

rxa-materials

The projects and the materials that accompany the Reactive Programming with Kotlin Book
Shell
35
star
51

MyRWTutorial

A reusable starter project for raywenderlich.com tutorials
Swift
34
star
52

video-jcomp-materials

The projects and the materials that accompany the Jetpack Compose course
Kotlin
34
star
53

sat-materials

The projects and materials that accompany the SwiftUI Animations by Tutorials book
Swift
32
star
54

dag-materials

The projects and the materials that accompany the Dagger by Tutorials book
Kotlin
31
star
55

bkk-materials

Kodeco Book Materials Template Repo
Shell
30
star
56

con-materials

About The projects and the materials that accompany the Concurrency by Tutorials book
Shell
28
star
57

RWDevCon-App

The RWDevCon app
Swift
28
star
58

sda-materials

The projects and the materials that accompany the Saving Data on Android book
Kotlin
28
star
59

wos-materials

The projects and the materials that accompany the watchOS With SwiftUI by Tutorials book
Swift
27
star
60

recipes

27
star
61

fpk-materials

The projects and materials that accompany the Functional Programming in Kotlin by Tutorials book
Kotlin
26
star
62

pasi-materials

The projects and the materials that accompany the iOS App Distribution & Best Practices book.
Swift
26
star
63

agit-materials

The projects and the materials that accompany the Advanced Git Book
Shell
25
star
64

SC_SusmitaHorrow

Swift
24
star
65

dabb-materials

The projects and the materials that accompany the Dart Apprentice: Beyond the Basics book
Dart
24
star
66

gita-materials

The projects and the materials that accompany the Git Apprentice book
Shell
24
star
67

not-materials

The projects and the materials that accompany the Push Notifications by Tutorials book
Swift
22
star
68

maca-materials

The projects and materials that accompany the macOS Apprentice book
Swift
21
star
69

RWAndroidTutorial

A reusable starter project for raywenderlich.com Android tutorials.
Kotlin
20
star
70

programmer-jokes

Sample repository for the Git Apprentice book
19
star
71

video-ps1-materials

The projects and the materials that accompany the Programming in Swift: Fundamentals course
Swift
19
star
72

alt-materials

The projects and materials that accompany the Auto Layout by Tutorials book
Swift
19
star
73

aat-materials

The projects and materials that accompany the Android Animations by Tutorials book
Kotlin
19
star
74

mlt-materials

The projects and the materials that accompany the Machine Learning by Tutorials book
Shell
18
star
75

video-insta-materials

The projects and the materials that accompany the How to Make an App Like Instagram in iOS video course
Swift
17
star
76

video-yfsa2-materials

Student Materials for Your First iOS and SwiftUI App: Polishing the App.
Swift
17
star
77

universal-links

HTML
17
star
78

video-comb-materials

The projects and the materials that accompany the Reactive Programming in iOS with Combine course
Swift
16
star
79

cat-materials

The projects and the materials that accompany the Catalyst by Tutorials Book
Swift
16
star
80

video-sdios-materials

The projects and the materials that accompany the Saving Data in iOS course
Swift
15
star
81

video-suif-materials

The projects and the materials that accompany the SwiftUI Fundamentals course
Swift
15
star
82

uapp-materials

The projects and the materials that accompany the Unity Apprentice book
C#
15
star
83

android-bootcamp-summer-2020

Homework solutions for the Android Bootcamp, class of Summer 2020.
Kotlin
15
star
84

video-nurls-materials

The projects and the materials that accompany the Networking with URLSession video course
Swift
15
star
85

kodeco-android-cookiecutter-template

A cookiecutter ๐Ÿช template for bootstrapping new Android Tutorial projects for Kodeco!
Kotlin
14
star
86

ideas

The "ideas" repository for the raywenderlich.com book Mastering Git
14
star
87

video-ukf-materials

The projects and the materials that accompany the UIKit Fundamentals video course
Swift
13
star
88

git-materials

The projects and the materials that accompany the Mastering Git Book
Shell
13
star
89

video-tv-materials

The projects and the materials that accompany the Table Views video course
Swift
12
star
90

rwdevcon-materials

Required materials for RWDevCon attendees.
12
star
91

video-jca-materials

The projects and materials that accompany the Jetpack Compose Animations course
Kotlin
12
star
92

video-iosa-materials

The projects and the materials that accompany the UIKit Animation course
Swift
12
star
93

saf-materials

The projects and the materials that accompany the Swift Apprentice:Fundamentals book.
Swift
12
star
94

video-kf1-materials

The projects and the materials that accompany the Kotlin Flow: Getting Started video course
Kotlin
11
star
95

DadJokes

A way to improve the programmer's day.
Swift
11
star
96

video-aidp-materials

The projects and materials that accompany the Advanced iOS Design Patterns course
Swift
11
star
97

video-abp-materials

The projects and the materials that accompany the Android Background Processing video course
Kotlin
10
star
98

adf-materials

The projects and the materials that accompany the Android Debugging by Tutorials book
Kotlin
10
star
99

video-yffa-materials

The projects and the materials that accompany the Your First Flutter App: An App From Scratch course
Dart
10
star
100

video-sli-materials

The projects and the materials that accompany the SwiftUI: Layout & Interfaces video course
Swift
10
star