OGeek|极客世界-中国程序员成长平台

标题: ios - Sprite Kit 是否会发布 Unseen Sprites [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:36
标题: ios - Sprite Kit 是否会发布 Unseen Sprites

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

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

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

- (void)addPhysicsBallAtLocationCGPoint)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 的低水平。所以我的问题是,性能下降的根本原因是什么?

我认为有几件事可能是原因。



Best Answer-推荐答案


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

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

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

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

-(void)updateCFTimeInterval)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4