减少 Spritekit 中的延迟

Posted

技术标签:

【中文标题】减少 Spritekit 中的延迟【英文标题】:Reduce lagg in Spritekit 【发布时间】:2018-09-04 22:30:51 【问题描述】:

我在使用 Spritekit 制作的游戏中出现了一些延迟。我相信问题是我在“spawn”SKAction.run 块中做了很多事情。问题是,我不知道该放在哪里。我曾尝试在 createClouds 函数之外创建 leftcloud 和 rightcloud,但这会导致应用程序崩溃……有人知道该怎么做吗?这会减少延迟吗?

let spawn = SKAction.run (
                () in
                self.createClouds()
            )

let delay = SKAction.wait(forDuration: 1.2)
let spawnDelay = SKAction.sequence([spawn, delay])
let spawnDelayFE = SKAction.repeatForever(spawnDelay)
let waitSpawnDelayFE = SKAction.sequence([SKAction.wait(forDuration: 2), spawnDelayFE])
self.run(waitSpawnDelayFE)

let distance = CGFloat(self.frame.height)
let moveClouds = SKAction.moveBy(x: 0, y: -distance, duration: TimeInterval(0.0055 * distance))
let removeClouds = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveClouds, removeClouds])

func createClouds()

    cloudpair = SKNode()

    let leftCloud = SKSpriteNode(imageNamed: "cloud2")
    let rightCloud = SKSpriteNode(imageNamed: "cloud2")

    leftCloud.physicsBody = SKPhysicsBody(texture: leftCloud.texture!, size: leftCloud.size)
    leftCloud.physicsBody?.isDynamic = false
    leftCloud.setScale(0.5)
    leftCloud.physicsBody?.affectedByGravity = false

    rightCloud.physicsBody = SKPhysicsBody(texture: rightCloud.texture!, size: rightCloud.size)
    rightCloud.physicsBody?.isDynamic = false
    rightCloud.setScale(0.5)
    rightCloud.physicsBody?.affectedByGravity = false

    leftCloud.position = CGPoint(x: self.frame.width / 2 - 160, y: self.frame.height)
    rightCloud.position = CGPoint(x: self.frame.width / 2 + 160, y: self.frame.height)

    cloudpair.addChild(leftCloud)
    cloudpair.addChild(rightCloud)

    cloudpair.run(moveAndRemove, withKey: "moveclouds")
    cloudpair.name = "cloudpair"
    cloudpair.addChild(scoreNode)
    cloudpair.zPosition = 3
    self.addChild(cloudpair)

    if cloudpair.position.x > 110 
        rightCloud.removeFromParent()
     else if cloudpair.position.x < -110 
        leftCloud.removeFromParent()
    

【问题讨论】:

【参考方案1】:

您正在动态创建物理实体,这很昂贵,并且可能是导致您滞后的原因。

我要做的是在场景加载时创建一个数组云对象,然后在需要云时循环它们。通过这种方式,在场景加载时创建对象,您可以立即访问而没有延迟

private var leftClouds = [SKSpriteNode]()
private var rightClouds = [SKSpriteNode]()
private var currentCloundIndex = 0

func createClouds() 

    //assuming your left and right clouds are different
    for x in 0..<10 
        let leftCloud = SKSpriteNode(imageNamed: "cloud2")
        leftCloud.physicsBody = SKPhysicsBody(texture: leftCloud.texture!, size: leftCloud.size)
        leftCloud.physicsBody?.isDynamic = false
        leftCloud.setScale(0.5)
        leftCloud.physicsBody?.affectedByGravity = false
        leftClouds.append(leftCloud)
    

    for x in 0..<10 
        let rightCloud = SKSpriteNode(imageNamed: "cloud2")
        rightCloud.physicsBody = SKPhysicsBody(texture: rightCloud.texture!, size: rightCloud.size)
        rightCloud.physicsBody?.isDynamic = false
        rightCloud.setScale(0.5)
        rightCloud.physicsBody?.affectedByGravity = false
        rightClouds.append(rightCloud)
    

然后,当您需要云时,只需使用 currentCloundIndex 将其从数组中拉出,然后递增 currentCloundIndex

let leftCloud = leftClouds[currentCloundIndex]
let rightCloud = rightClouds[currentCloundIndex]
currentCloundIndex += 1

if currentCloundIndex >= 10 
    currentCloundIndex = 0

【讨论】:

没问题,遇到过基本一样的问题

以上是关于减少 Spritekit 中的延迟的主要内容,如果未能解决你的问题,请参考以下文章

带有 Skview 的 ViewController - SpriteKit SKScene

在 SpriteKit 项目中包含一个 UIKit 小游戏

在 SpriteKit 中为函数添加延迟

调用函数 touchesBegan 时 SpriteKit 游戏延迟

SpriteKit - 可以使用 SDWebImage 将 Facebook 朋友的图像延迟加载到 spriteNodeWithTexture 中吗?

touchesMoved中的SpriteKit + AVAudioPlayer:杀死fps