减少Spritekit中的延迟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了减少Spritekit中的延迟相关的知识,希望对你有一定的参考价值。
我在Spritekit中制作的游戏中得到了一些lagg。我相信问题是我必须在“spawn”SKAction.run块中进行大量工作。问题是,我不知道还能把它放在哪里。我尝试在createClouds-function之外创建leftcloud和rightcloud,但这导致应用程序崩溃...有谁知道该怎么做?这会减少lagg吗?
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
}
以上是关于减少Spritekit中的延迟的主要内容,如果未能解决你的问题,请参考以下文章
调用函数 touchesBegan 时 SpriteKit 游戏延迟