Sprite 套件精灵加载性能改进 [英] Sprite kit sprites loading performance improvement

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

问题描述

我正在制作一个精灵套件游戏,我正在使用 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 套件精灵加载性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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