forked from bloominstituteoftechnology/ios-sprite-utility1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SKSpriteNode+Utility.swift
30 lines (23 loc) · 956 Bytes
/
SKSpriteNode+Utility.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import SpriteKit
extension SKSpriteNode {
static let textureKey = "Texture Key"
// Load textures for a sprite node and run that sequence forever
func loadTextures(named name: String, forKey key: String) {
// Load atlas
let atlas = SKTextureAtlas(named: name)
// Collect and sort textures
let textures = atlas.textureNames
.sorted(by: <)
.map({ atlas.textureNamed($0) })
// Need at least one texture to continue
guard !textures.isEmpty else { return }
// Assign size
self.size = textures[0].size()
// Remove any prevous texture sequence
self.removeAction(forKey: key)
// Run texture sequence forever
let action = SKAction.animate(with: textures, timePerFrame: 0.03333)
let foreverAction = SKAction.repeatForever(action)
self.run(foreverAction, withKey: key)
}
}