通过touchesMoved解决FPS下降问题?

时间:2018-08-06 22:05:52

标签: swift sprite-kit touchesmoved cadisplaylink

在我的SpriteKit游戏中,我有两个朝触摸点旋转的精灵。另外,我还有敌人的精灵从高处掉落,并随着时间的推移以越来越高的频率向下传播。在连续触摸约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

    }
}

0 个答案:

没有答案