gpt4 book ai didi

swift - 减少 Spritekit 中的延迟

转载 作者:行者123 更新时间:2023-11-28 07:41:17 25 4
gpt4 key购买 nike

我在使用 Spritekit 制作的游戏中遇到了一些问题。我认为问题在于我在“生成”SKAction.run block 中进行了很多操作。问题是,我不知道还能把它放在哪里。我曾尝试在 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
}

关于swift - 减少 Spritekit 中的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com