swift - 使用 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
}
}
解决方案
推荐阅读
- c++ - 将 void* 强制转换为具有多重继承的类
- phpstorm - 从 PhpStorm 中查找 GIT 存储库
- visual-studio-2017 - SSIS:“找不到 SQLIPackage140”
- javascript - 如何删除 lodash 模板创建的换行符?
- python - Tensorflow AttributeError:类型对象'numpy.ndarray'没有属性'__array_function__'
- ios - 重新加载 UITableView 除了它的一部分
- c# - Xamarin IOS:BLE
- mariadb - 为什么一个简单的 ALTER 语句会导致 galera 崩溃
- java - Jenkins + Maven 发布插件:release:branch + Git - 无法提交文件
- r - 将 YYYYMM 因子格式转换为 YYYY-MM 格式