减少 Spritekit 中的延迟

Reduce lagg in Spritekit

我在用 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()
    }
}

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

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

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
}