title: Changing a `SKSpriteNode.texture` without a flicker
body: <p>I have a <code>SKSpriteNode</code> that needs to change the <code>texture</code>, since in my 2d isomorphic game it needs to use different frames when the object is facing different directions. At first I accomplished this quite simply: <code>self.sprite.texture = SKTexture(imageNamed: frameName)</code>, but I found that this caused a "flicker" where the sprite turned invisible for a moment (presumably while loading up the new texture).</p>
  
  <p>I decided to implement a static cache of <code>SKTexture</code> objects to avoid re-loading from disk. It works well, but it still suffers from the same flicker problem <strong>only the first time each frame is used</strong>. Check out <a href="http://inzania.com/temp/jittery.mov" rel="nofollow">this video</a> to see what I mean. Source code:</p>
  
  <pre><code>class GameObject : GameSprite {
    static var textureCache:[String:SKTexture] = [:]
  
    func setup() {
      self.sprite = SKSpriteNode(imageNamed: assetName)
      for dir in Direction.directions {
        let textureName = self.assetPrefix + dir.description.lowercaseString
        GameObject.textureCache[textureName] = SKTexture(imageNamed: textureName)
      }
    }
  
    var assetName:String {
      return self.assetPrefix + self.direction.description.lowercaseString
    }
  
    var direction:Direction {
      set {
        if self.dir != newValue.rawValue {
          self.dir = newValue.rawValue
          self.sprite.texture = GameObject.textureCache[assetName]
        }
      }
      get { return Direction(rawValue: self.dir)! }
    }
  
    // ...
  }
  </code></pre>