I'm building a game in SpriteKit and have recently ran into problems with my frame rate continually dropping to 50-45 from 60 only in GameScene
and a couple second lag upon opening the GameScene
from my StartScene
. I have tried a bunch of different things and I believe one of my main problems has to do with animating a SKSpriteNode subclass that spawns very frequently, when I stop animating them and instead use a single texture the frame rate stays at 60. I previously posted about preloading textures into SpriteKit, and received awesome answers but I may have not fully understood. At this point I have all of my GameScene
textures in a single Atlas and I load them all in by creating constants for each right before my GameScene
:
//Preload textures of GameScene before running it
let alienTexture1 = SKTextureAtlas(named:"Sprites").textureNamed("alienTexture1")
let alienTexture2 = SKTextureAtlas(named:"Sprites").textureNamed("alienTexture2")
...
class GameScene: SKScene, SKPhysicsContactDelegate {
I know that I'm supposed to use:
SKTextureAtlas(named: "YourTextureAtlasName").preloadWithCompletionHandler {
// Now everything you put into the texture atlas has been loaded in memory
}
to preload the textures but I'm not sure what goes in between the brackets and when a line like let alienTexture1 = SKTextureAtlas(named:"Sprites").textureNamed("alienTexture1")
is supposed to be called. These textures are then called inside of the init
of some Alien SKSpriteNode subclasses that I have. I'm not understanding something because the preload clearly isn't working properly. I'm also not sure what to do about the lag when switching to the GameScene
but I imagine it also has to do with my way of loading in textures at the beginning with all of those let
statements. Any advice would be great.
I have looked at other questions on this subject and felt that this was specific enough that it wasn't a repeat.