Sprite kit精灵加载性能提升 [英] Sprite kit sprites loading performance improvement

查看:89
本文介绍了Sprite kit精灵加载性能提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作精灵工具包游戏,我正在使用plist文件来设置每个级别的属性。我的plist文件中的一个属性是一个名为patterns的字典,它包含n个项目,其中每个项目都是一个块,带有手动键入的x和y位置。这个模型对于我正在进行的游戏来说非常合适,因为以快速的方式设置关卡非常方便。然而,由于缺乏编码经验,我面临一个我无法解决的缺点:一些级别有多达290个块,所以当应用程序尝试读取级别时,应用程序会冻结5秒钟。这对用户来说非常烦人。在开始时我的方法是:读取plist文件,并为每个项目调用使用其imageNamed方法创建块作为SKSpriteNode的方法。我认为这是它滞后的原因,我试图在运行时加载300个精灵的事实似乎是问题的一个有希望的原因。然后我尝试了以下内容:当游戏第一次启动时,我创建了最初加载块池的方法。这是我的方法

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)

    }

}

然后,我的代码读取plist文件,并且每个该块调用以下内容:

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. 

我也有方法处理新元素应该作为游戏收益添加到池中的事实一切正常。滞后时间下降到3.5到4秒左右,但这显然还不够好。我希望有一个没有滞后的游戏。但是,我不确定是否有另一种更有效的方法来做我想要做的事情而不是使用精灵池。
有谁知道如何减少这个滞后时间?

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?

推荐答案

我遇到了同样的问题!问题出在这一行...

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

let element = SKSpriteNode(imageNamed: "obsticle1")

SpriteKit不够聪明,不知道已经使用该图像创建了纹理。所以它正在做的是一遍又一遍地创造这种纹理,这是昂贵的。

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.

这篇关于Sprite kit精灵加载性能提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆