Search code examples
iossprite-kitxcode8game-physicsscene

How to load files in memory for better scene transition?


When I transition to my gamescene there is a huge lag and the FPS drop very low. I know for a fact that is due to the large images i have in my scenes .The images are used for Skspritenodes from Assets. I am looping through one scene. When the player loses the game restarts by transitioning to the same scene. Is there anyway I can my load the images in memory and keep them there forever instead of them getting deleted and loaded every time I move to gamescene ?

If you are going to suggest textures and atlas preloading. Please be specific on how and where to use preload code. Also how and to set up atlas texture and use them. I have already tried this approach and unfortunately it doesnt work. Perhaps I am doing it wrong.

Thank you,


Solution

  • Picking up from the comments, there are multiple ways to break up loading. Here is one that popped in my head:

    class Scened: SKScene {
    
      var needsLoading = false
    
      func loadChunk1() {}
      func loadChunk2() {}
      func loadChunk3() {}
      func loadChunk4() {}
      func loadChunk5() {}
    
      override func didMove(to view: SKView) {
        if needsLoading { loadChunk1() }
      }
      override func didEvaluateActions() {
        if needsLoading { loadChunk2() }
      }
      override func didSimulatePhysics() {
        if needsLoading { loadChunk3() }
      }
      override func didApplyConstraints() {
        if needsLoading { loadChunk4() }
      }
      override func didFinishUpdate() {
        if needsLoading {
          loadChunk5()
          // Disable loading after the last chunk:
          needsLoading = false
        }
      }
    }
    

    enter image description here