首页 > 解决方案 > 使用 touchesMoved 解决 FPS 下降问题?

问题描述

在我的 SpriteKit 游戏中,我有两个向触摸点旋转的精灵。此外,我的敌人精灵从顶部掉落并随着时间的推移以更高的频率向下移动。在使用 touchesMoved 连续触摸大约 30 秒后,随着帧速率下降到 30,精灵旋转和敌人精灵移动变得非常不稳定。我认为这是由于主线程上的图形处理过多造成的。有没有办法通过 touchesBegan 和 touchesMoved 中的 CADisplayLink 处理精灵旋转?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    for touch: AnyObject in touches {
        location = touch.location(in: self)
        let DegreesToRadians = Pi / 180

        let rightDeltaX = location.x - rightSprite.position.x
        let rightDeltaY = location.y - rightSprite.position.y
        let rightAngle = atan2(rightDeltaY, rightDeltaX)

        let leftDeltaX = location.x - leftSprite.position.x
        let leftDeltaY = location.y - leftSprite.position.y
        let leftAngle = atan2(leftDeltaY, leftDeltaX)

        leftSprite.zRotation = leftAngle - 90 * DegreesToRadians

        rightSprite.zRotation = rightAngle - 90 * DegreesToRadians

        }
    }
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        location = touch.location(in: self)
        let DegreesToRadians = Pi / 180

        let rightDeltaX = location.x - rightSprite.position.x
        let rightDeltaY = location.y - rightSprite.position.y
        let rightAngle = atan2(rightDeltaY, rightDeltaX)

        let leftDeltaX = location.x - leftSprite.position.x
        let leftDeltaY = location.y - leftSprite.position.y
        let leftAngle = atan2(leftDeltaY, leftDeltaX)

        leftSprite.zRotation = leftAngle - 90 * DegreesToRadians

        rightSprite.zRotation = rightAngle - 90 * DegreesToRadians

    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        location = touch.location(in: self)

        leftSprite.zRotation = 0

        rightSprite.zRotation = 0

    }
}

标签: swiftsprite-kittouchesmovedcadisplaylink

解决方案


推荐阅读