Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

ios - Sprite kit sprites loading performance improvement

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 contains n items, where each of the items is a block, with hand typed x and y positions. This model is working perfectly fine for the kind of game I am making, as it is very convenient to set the levels right in a quick manner. However, I am facing one drawback I cant solve myself due to the lack of coding experience: some of the levels have as many as 290 blocks, so when the app tries to read the level, the app freezes for like 5 seconds. This is very annoying for the user. At the beginning my approach was: Read the plist file, and for each item call the method which creates the block as a SKSpriteNode using its imageNamed "" method. I thought this is the reason it lags so much, the fact that I am trying to load 300 sprites at the runtime seemed as a promising cause of the problem. Then I tried the following: I made the method which loads a pool of block initially, when the game starts for the first time. This is my method for that

    func addObsticles1ToPool() {

    for i in 0...300 {

       let element = SKSpriteNode(imageNamed: "obsticle1")
       element.hidden = true
       obsticle1Pool.append(element)

    }

}

Then, my code reads the plist file, and for each of the block calls the following:

func block(x: CGFloat, y: CGFloat, movingUp: Bool, movingSide: Bool, spin: Bool, type: Int16) {

    var block: SKSpriteNode!
    for obs in obsticle1Pool {

        if obs.hidden {

            block = obs
            break

        }

    }


  block.hidden = false
  // Further down the properties of the block are set, such as actions it should perform depending on the input values, also its physics body is set. 

I also have methods handling the fact that new elements should be added to the pool as game the proceeds and all that works just fine. The lag time dropped to around 3.5 - 4 secs, but that is still not good enough obviously. I would like to have a game with no lag. However, I am not sure if there is another, more efficient way, to do what I am trying to do than using the sprites pool. Does anyone know how to reduce this lag time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...