Sprite kit sprites loading performance improvement

后端 未结 1 1058
失恋的感觉
失恋的感觉 2020-12-09 23:31

I am making a sprite kit game and I am using the plist file to set properties of each level. One of the properties in my plist file is a dictionary called patterns, which co

相关标签:
1条回答
  • 2020-12-10 00:28

    I have had the same problem! The issue is in this line...

    let element = SKSpriteNode(imageNamed: "obsticle1")
    

    SpriteKit isn't smart enough to know that a texture was already created with that image. So what it is doing is creating that texture over and over again and that is expensive.

    Instead create a texture outside of the loop first and then create the sprite node with a texture. Something like this...

    let elementTexture = SKTexture(imageNamed: "objstical1")
    
    for i in 0...300 {
    
        let element = SKSpriteNode(texture: elementTexture)
        element.hidden = true
        obsticle1Pool.append(element)
    
    }
    

    Not only will this be a ton faster it will decrease your apps memory a ton...assuming it was the same issue I was having. Hopefully that helps.

    0 讨论(0)
提交回复
热议问题