Skip to content

Instantly share code, notes, and snippets.

@veeneck
Created January 5, 2015 18:04
Show Gist options
  • Save veeneck/f1d534a87133415bfd6d to your computer and use it in GitHub Desktop.
Save veeneck/f1d534a87133415bfd6d to your computer and use it in GitHub Desktop.
Update Loop Service
import SpriteKit
class UpdateService {
/// Handle on current SKScene
let scene : Level
/// Array of callbacks to be called each time the updateLoopRuns
var updateListeners : Array<((CFTimeInterval) -> ())> = []
init(scene: Level) {
self.scene = scene
}
/// Add a callback function to the update cache.
func addUpdateListener(listener:((CFTimeInterval) -> ())) {
self.updateListeners.append(listener)
}
/// Reset the update cache.
func clearUpdateCache() {
self.updateListeners = []
}
/// Loop over desired items, update them, and then add callback to cache.
func update(currentTime: CFTimeInterval) {
if self.updateListeners.count > 0 {
for listener in self.updateListeners {
listener(currentTime)
}
}
else {
/// Update CharacterNodes
self.scene.layers[WorldLayer.World.rawValue].enumerateChildNodesWithName("Character") {
node, stop in
let realNode = node as CharacterNode
realNode.updateWithTime(currentTime)
self.addUpdateListener(realNode.updateWithTime)
}
/// Update Heraldry
self.scene.layers[WorldLayer.World.rawValue].enumerateChildNodesWithName("Heraldry") {
node, stop in
let realNode = node as Heraldry
realNode.updateWithTime(currentTime)
self.addUpdateListener(realNode.updateWithTime)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment