ios - Sprite Kit 是否发布了 Unseen Sprites

标签 ios objective-c sprite-kit

在我的 Sprite Kit 应用程序中,在执行达到某个点后,我遇到了突然的延迟峰值。我相信我已将问题区域缩小到 SKScene 子类中的以下代码片段。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint loc = [[touches anyObject] locationInView:self.view];
    [self addPhysicsBallAtLocation:loc];
    /* included this to show that -addPhysicsBallAtLocation: 
       is being called many times */
}

结合方法-addPhysicsBallAtLocation的以下实现:

- (void)addPhysicsBallAtLocation:(CGPoint)location
{
    SKSpriteNode *ball = [[SKSpriteNode alloc] initWith...]; // just size + color
    /* I add a few properties such as physicsBody to the node. */
    [self addChild:ball];
}

由于重力,创建的节点飞离屏幕并且不再可见。当他们离开屏幕时,nodeCount 会回落到正常水平。尽管 nodeCount 处于应有的位置,但一段时间后,场景将出现滞后,每秒帧数将急剧下降。一旦发生这种性能下降,每秒帧数就永远不会恢复并保持在 15 fps 的低水平。所以我的问题是,性能下降的根本原因是什么?

我认为可能有一些原因。

  • Sprite Kit 在 Sprite 离开屏幕后不会释放它们。 (大概是这个)
  • 未发布的 Sprite 仍在绘制中,就在屏幕外。 (可能是一个因素)
  • Sprite 不再可见后,重力计算仍在进行。 (增加滞后)

最佳答案

您是对的,因为您列出的原因可能是导致丢帧的罪魁祸首。

有一件事是,未发布的 Sprite 根本不会被绘制(屏幕上的节点数代表正在绘制的节点)。

在我自己的项目中,我在-update:方法中处理过类似的情况。

对于此示例,我假设您已将每个 Ball 节点的 name 属性设置为 @"ball"

-(void)update:(CFTimeInterval)currentTime
{
    //Removing ball nodes when they have reached edge of screen
    [self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.x < 0 || node.position.x > self.frame.size.width || node.position.y > self.frame.size.height || node.position.y < 0)
        {
            [node removeFromParent];
        }
    }];
}

关于ios - Sprite Kit 是否发布了 Unseen Sprites,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379496/

相关文章:

ios - 向 UINavigationBar 添加图像

ios - AVPlayer 导致 TableView 滞后

objective-c - 以编程方式将 UIButton 添加到 UICollectionView 单元格

iphone - 我自己的目录的位置

ios - SKNode : Converting Coordinates from UIView to SKNode 上的 UITapGestureRecognizer

ios - 延迟加载 UIScrollView 或 UITableView

ios - 搜索栏处于事件状态时,快速 TableView 被锁定

objective-c - 调整CPScrollView的内容大小

swift - 如何在 spritekit swift 中制作一个旋转角度的正弦?

ios - 为什么无法进行物体内碰撞检测?