我有一个用 SpriteKit 构建的游戏,我偶尔会玩它。当 iOS 8 还是 iOS 的最新版本时,我就开始研究它了。当时,它总是以 60 fps 或几乎(在物理设备上)的速度运行。然而,从 iOS 9 开始,现在在 iOS 10 上,游戏在我的 iPhone 5 上的运行速度只有区区 12 fps。实际上,它在模拟器上通常运行得更快(13 - 14 fps),这是闻所未闻的。
我使用 Instruments 进行了分析,看来部分缓慢是由于应用程序每帧执行两次枚举。它们在这里:
-(void)moveBackground
{
[self enumerateChildNodesWithName:@"stars" usingBlock:^(SKNode *node, BOOL *stop)
{
SKSpriteNode *bg = (SKSpriteNode *)node;
CGPoint bgVelocity = CGPointMake(0, -150); //The speed at which the background image will move
CGPoint amountToMove = CGPointMultiplyScalar (bgVelocity,0.08);
bg.position = CGPointAdd(bg.position,amountToMove);
if (bg.position.y <= 0)
{
bg.position = CGPointMake(bg.position.x, bg.position.y + bg.size.height/2);
}
}];
[self enumerateChildNodesWithName:@"opbg" usingBlock:^(SKNode *node, BOOL *stop)
{
SKSpriteNode *opbg = (SKSpriteNode *)node;
CGPoint bgVelocity = CGPointMake(0, -150); //The speed at which the background image will move
CGPoint amountToMove = CGPointMultiplyScalar (bgVelocity,0.08);
opbg.position = CGPointAdd(opbg.position,amountToMove);
if (opbg.position.y <= 0)
{
[self.opbg removeFromParent];
}
}];
}
这些枚举将起始背景移动到屏幕外,然后将其从父背景中删除,并在完成后循环常规背景。有没有更有效的方法来做到这一点?此外,是否有另一种方法可以在不损失任何质量的情况下优化应用程序?我的图像相对较大(背景为 1080x3840),但我不确定是否可以使用较小的图像,然后在不损失质量的情况下放大它们。
感谢任何提高我的帧速率的帮助,如果需要,我可以显示更多代码。
谢谢
最佳答案
iOS 8 和 9 之间最大的变化是添加了相机节点:
https://developer.apple.com/reference/spritekit/skcameranode
以及臭名昭著的性能下降。
Apple 论坛上有很多关于 iOS 9 性能的提示。Apple 没有及时、令人满意地进行沟通。由于缺乏有意义的修复和解释,许多 SpriteKit 用户和潜在用户流失了。
就您而言,切换到使用相机来处理视差可能会恢复一些性能。
以下是正在讨论的一些有关性能的问题,因此您可能会看到是否有人在做类似的事情:
关于ios - SpriteKit 游戏中的帧速率极慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41819721/