【问题标题】:Slow down in iOS 7 in Cocos2D with transparent background在 Cocos2D 中使用透明背景在 iOS 7 中减速
【发布时间】:2013-11-08 19:04:46
【问题描述】:

我已经在 Cocos2D 中开发一款游戏大约 3 年了,它利用透明背景来显示 UIView。这样做的原因是让视差背景仍然像 Cocos2D 进行场景转换一样运行。

当我更新到 iOS 7 时,我遇到了一个新问题。在以下情况下会出现速度变慢:

-仅当视差背景的帧位置发生变化时。

-如果我消灭一个会发出小精灵和粒子效果的敌人。

所以这是这两件事的结合,它只是有时会发生。发生减速时,帧速率的调试值不会下降。如果我加载一个新场景,它会恢复正常。有时当我摧毁另一个敌人时,减速也会消失。

我的视差 UIView 中的代码几乎运行在游戏中的每一帧。我将问题总结为一行:

-(void)updateImagePosWithPos:(CGPoint)pos{ // in game

    // create vel based on last currentPos minus new pos

    CGPoint vel = CGPointMake(currentPos.x-pos.x, currentPos.y-pos.y);

    // init variables tmpVel and tempTotalImages

    CGPoint tmpVel = CGPointZero;

    int tmpTotalImages = 0;

    // create indexLayerArr

    NSMutableArray *indexLayerArr = [NSMutableArray array];

    // for every parallax layer, add the number of images horizontally minus 1 to indexLayerArr

    for (int j=0; j<totalLayers; ++j){

        [indexLayerArr addObject:[NSNumber numberWithInt:[[totalImagesArr objectAtIndex:j] intValue]-1]];

    }

    int i = 0;


    for (UIImageView *imageView in self.subviews) {

        CGRect tmpRect = CGRectZero;

        NSMutableArray *tmpRectArr = [rectContainer objectAtIndex:imageView.tag];

        float speed = 0.00;


        tmpTotalImages = [[totalImagesArr objectAtIndex:imageView.tag] intValue];

        speed = [[speedArr objectAtIndex:imageView.tag] floatValue];

        tmpVel = CGPointMake(vel.x*speed, vel.y*speed);

        i = [[indexLayerArr objectAtIndex:imageView.tag] intValue];

        tmpRect = [[tmpRectArr objectAtIndex:i] CGRectValue];


        if(tmpRect.origin.x - tmpVel.x > wins.width){

            tmpRect.origin.x -= (tmpTotalImages)*tmpRect.size.width;

        }
        else if(tmpRect.origin.x - tmpVel.x < -tmpRect.size.width){

            tmpRect.origin.x += (tmpTotalImages)*tmpRect.size.width;

        }

        tmpRect.origin.x -= tmpVel.x;
        tmpRect.origin.y += tmpVel.y;

        [tmpRectArr replaceObjectAtIndex:i withObject:[NSValue valueWithCGRect:tmpRect]];


        imageView.frame = [[tmpRectArr objectAtIndex:i] CGRectValue]; // <-- slow down cause



        i--;

        [indexLayerArr replaceObjectAtIndex:imageView.tag withObject:[NSNumber numberWithInt:i]];


    }

    currentPos = CGPointMake(pos.x, pos.y);

}

见注释行 imageView.frame = [[tmpRectArr objectAtIndex:i] CGRectValue];

因此,如果我注释掉该行,问题将永远不会发生。如果我保留这条线并且不更改 tempRect 的值,那么问题也不会发生。

在 iOS 7 中更改 UIImageView 的框架位置似乎存在问题,但只是有时。只是想知道我可以使用哪些其他替代品?还是我在 iOS 7 中做错了什么?

【问题讨论】:

  • 这是发生在设备、模拟器还是两者上?
  • 设备上发生了
  • 不幸的是我没有答案,但我有一个建议,您可以将:imageView.frame = [[tmpRectArr objectAtIndex:i] CGRectValue]; 更改为 imageView.frame = tmpRect; 它应该为您节省数组查找,因为它看起来像 tmpRect 是无论如何,你要搬进来的价值。
  • 谢谢,我没注意到!我修复了它,但仍然会出现减速。
  • 您是否使用 Instruments 工具检查过任何泄漏?

标签: uiview cocos2d-iphone uiimageview ios7 slowdown


【解决方案1】:

不是解决您的问题的方法,而是解决方法。我将从可能需要更改代码最多的那个开始。

  1. 您实际上不必拥有 UIView 来保持背景与过渡。相反,如果您完全在 cocos2d 中实现背景(作为场景的一部分),则可以实现相同的效果,而不是替换场景,而是将图层转换进出。大多数情况下,场景转换使用同样适用于节点的动作。

  2. 使用cocos2d节点实现后台,并有一个父节点作为后台节点的容器(即“层”)。您可以对该节点执行以下两项操作之一:

    一个。编辑 CCDirectorIOS 的代码并添加对您的后台节点的引用。通过在 [_runningScene visit] 之前的后台节点上调用 visit,在 drawScene 方法中的所有其他节点之前更新节点。

    b.过渡到新场景时,要么从当前场景中移除背景节点并将其添加到新场景中,要么创建具有所有相同设置的背景副本并将其添加到新场景中。确保副本以与原件完全相同的状态开始。尽管由于动画的性质(即移动/翻转/缩放),这不适用于大多数过渡。

  3. 如果您需要在过渡运行时为背景设置动画,有一个简单的技巧。在具有全局生命周期的非 CCNode 对象(即 AppDelegate)上安排更新。然后手动将更新发送到应在过渡期间继续更新其状态的所有节点,并且仅在过渡期间。

您可以像这样在非节点对象上注册更新:

[_director.scheduler scheduleUpdateForTarget:self priority:0 paused:NO];

即使在场景转换期间也会调用此更新方法。或者,也应该可以通过更改节点的paused 状态来继续更新节点,从而恢复调度程序和操作,方法是覆盖 paused 属性或在发生转换时显式取消暂停特定节点。

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多