cocos2d项目中的CPU问题 - 简单的项目运行速度非常慢

时间:2014-03-09 08:32:04

标签: ios objective-c cocos2d-iphone box2d

感谢您抽出时间阅读本文!

我在我的项目中使用了Cocos2d和Box2d。该项目的逻辑非常简单。玩家只是射击敌人。如果子弹击中敌人,子弹以及敌人将被摧毁。如果任何敌人在没有射击的情况下穿过屏幕,则游戏结束。部分代码如下:

- (void)birdDone:(ZLBird*) birdToDelete {

CCSprite *sprite = (CCSprite *)birdToDelete;

b2Body *spriteBody = NULL;
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        CCSprite *curSprite = (__bridge CCSprite *)b->GetUserData();
        if (sprite == curSprite) {
            spriteBody = b;
            break;
        }
    }
}

if (spriteBody != NULL) {
    world->DestroyBody(spriteBody);
}
[sprite removeFromParentAndCleanup:YES];
sprite = NULL;
}


-(void) update:(ccTime)delta{

SOME CODE HERE

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
    CCSprite *myActor = (__bridge CCSprite*)b->GetUserData();
    if (b->GetUserData() != NULL)
    {
        //Synchronize the AtlasSprites position and rotation with the corresponding body
        myActor.position = (CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));
        myActor.rotation = (-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
    }
}

// goes through all the rocks
for (ZLRock *rockToAct in rocksArr){
    [rockToAct incrementTime];
    if ([rockToAct getExistedTime] >= 400)
    {
        // Remove the rock several seconds after it has been shot
        [self rockDone:rockToAct];
    }
}

// goes through all the elements in BirdsArr
for (ZLBird *birdToAct in birdsArr){
    [birdToAct incrementTime];
    if ([birdToAct getDeadTime] >= 400 || birdToAct.position.x > WIDTH_WINDOW+20)
    {
        // Remove the bird several seconds after it has been dead
        [self birdDone:birdToAct];
    }

    // JUST FOR TEST
    if (birdToAct.position.x > WIDTH_WINDOW && !birdToAct.isDead)
        NSLog(@"YOU LOSSSSSSSS!");
}

当我在iphone上运行这个项目时,不知何故它变得非常慢。我检查了仪器上的CPU使用率,CPU使用率是70%。

为什么?我想,因为我启用了ARC,所以没有内存泄漏。是因为我在每个帧中经历了几个数组,这真的会让设备变慢吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

刚刚发现问题!

当我从父级删除它们时,我没有从数组中删除这些对象。因此,阵列大小不断增长,最终导致压垮。

感谢您的回答!