减少Spritekit的延迟

时间:2018-09-04 22:30:51

标签: swift sprite-kit skspritenode

我在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 个答案:

答案 0 :(得分:0)

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

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

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
}