gpt4 book ai didi

ios - 创建 spritenodes 时 Swift 游戏滞后

转载 作者:行者123 更新时间:2023-11-30 13:51:03 26 4
gpt4 key购买 nike

因此,我最近将一款游戏上传到应用商店,并且有用户报告在旧设备上出现延迟。首先是游戏:它被称为“CURVE”(我不确定我是否可以在这里发布它的名称,但它会帮助您理解问题)。游戏的想法是一个球在屏幕上移动并穿过墙壁之间的间隙。还有一些较小的球从主球上落下,从而形成某种路径。现在,滞后。它们发生在两个主要时刻。首先,当路径创建时,其次,当玩家穿过间隙时。当玩家穿过间隙时,他会与一个不可见的节点发生碰撞。记录碰撞,更新分数。然后该节点被删除,玩家继续。我相信当 swift 创建 SpriteNode 或删除 SpriteNode 时会出现延迟。关于如何处理这个问题有什么想法吗?

这是我在生成障碍时使用的代码 - 我认为这就是问题所在

func createRocks() {

rockHeight = (frame.height * (heightOfRocks))

if gameState != .Dead {

switch spawnCount {

case 10:
levelUp()

case 20:
levelUp()

case 30:
levelUp()

case 40:
levelUp()

case 50:
levelUp()

case 60:
levelUp()

case 70:
levelUp()

case 80:
levelUp()

case 90:
levelUp()

case 100:
levelUp()

case 110:
levelUp()

case 120:
levelUp()

case 130:
levelUp()

case 140:
levelUp()

case 150:
levelUp()

default:
break

}
}
++spawnCount






let leftRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
leftRock.fillColor = themeColor
leftRock.strokeColor = themeBorderColor
leftRock.lineWidth = themeBorderWidth

leftRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))



leftRock.physicsBody?.dynamic = false



let rightRock = SKShapeNode(rectOfSize: CGSize(width: (frame.width), height: (rockHeight)), cornerRadius: (cornerRad))
rightRock.fillColor = themeColor
rightRock.strokeColor = themeBorderColor
rightRock.lineWidth = (themeBorderWidth)


rightRock.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: ((frame.width) + (themeBorderWidth)), height: ((rockHeight) + (themeBorderWidth))))
rightRock.physicsBody?.dynamic = false


leftRock.zPosition = CGFloat(10 + (21 * levelNumber))
rightRock.zPosition = CGFloat(10 + (21 * levelNumber))


// 2
let rockCollision = SKSpriteNode(color: UIColor.clearColor(), size: CGSize(width: (frame.width * 2), height: 32))

rockCollision.physicsBody = SKPhysicsBody(rectangleOfSize: rockCollision.size)
rockCollision.physicsBody?.dynamic = false

rockCollision.name = "scoreDetect"

addChild(leftRock)
addChild(rightRock)
addChild(rockCollision)



// 3

let rockWidth = frame.width

let yPosition = frame.height + leftRock.frame.height

let rockDistance = frame.width * (gapWidth)

let min = Int(((frame.width) * (wallWidth)) - (rockWidth / 2))
let max = Int(((frame.width) * (1.00 - ((wallWidth)))) - rockDistance - (rockWidth / 2))


let rand = min + Int(arc4random_uniform(UInt32(max - min)))
//GKRandomDistribution(lowestValue: min, highestValue: max)

let xPosition = CGFloat(rand)



// 4
leftRock.position = CGPoint(x: xPosition, y: yPosition)
rightRock.position = CGPoint(x: xPosition + rockWidth + rockDistance, y: yPosition)


rockCollision.position = CGPoint(x: xPosition, y: CGRectGetMidY(frame))

rockCollision.position = CGPoint(x: CGRectGetMidX(frame), y: yPosition + (rockCollision.size.height * 2))





let endPosition = frame.height + (leftRock.frame.height * 2)



let moveAction = SKAction.moveByX(0, y: -endPosition, duration: timeOfRockMovement)
let moveSequence = SKAction.sequence([moveAction, SKAction.removeFromParent()])
leftRock.runAction(moveSequence)
rightRock.runAction(moveSequence)
rockCollision.runAction(moveSequence)
}
func initRocks() {



let create = SKAction.runBlock { [unowned self] in
self.createRocks()
}

let wait = SKAction.waitForDuration(rockSpawnWait)
let sequence = SKAction.sequence([create, wait])
let repeatForever = SKAction.repeatActionForever(sequence)

runAction(repeatForever)
}

最佳答案

您的方法听起来与 a SpriteKit tutorial I wrote 非常相似,但我在那里完全没有滞后。 SpriteKit 非常快,但您的具体条件可能会导致问题。检查事项:

1) 您使用纹理图集吗?这些图像比 bundle 中的单独图像加载速度更快。

2) 如果您经常重复使用图像,您可以将它们存储在 SKTexture 中,然后从那里加载节点吗?

3) 是否还有其他事情发生,例如播放声音? iOS 模拟器在播放游戏中的第一个声音时经常会出现短暂的延迟。

4)创建的路径是否很复杂?

关于ios - 创建 spritenodes 时 Swift 游戏滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256484/

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