制造敌人,减慢游戏Cocos2d

时间:2011-12-11 01:17:24

标签: iphone cocos2d-iphone

我正在做一场比赛,而我只有一个最后的问题。当游戏创造敌人时,FPS会减慢到40或20.取决于它是否会产生1或2个敌人。

NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 3; ++i) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"laser_%d.png", i]]];
    }

    CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.12f];    
    CCSprite *laser = [CCSprite spriteWithSpriteFrameName:@"laser_1.png"];

    int tempY = (arc4random() % ((int)(300 - laser.boundingBox.size.height))) + laser.boundingBox.size.height;
    float tempRot = (arc4random() % 30) + 1;
    int sign = (arc4random() % 2) - 1;
    if (sign < 0) {
        tempRot *= -1;
    }

    laser.tag = 2;

    laser.position = ccp(650, tempY);
    laser.rotation = CC_RADIANS_TO_DEGREES(tempRot);
    CCAction *walkAction = [CCRepeatForever actionWithAction:
                            [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:YES]];
    [laser runAction:walkAction];
    [spriteSheet addChild:laser];

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(laser.position.x/PTM_RATIO, 
                               laser.position.y/PTM_RATIO);
    spriteBodyDef.userData = laser;
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef);

    [[GB2ShapeCache sharedShapeCache] addShapesWithFile:@"laserBody.plist"];
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:spriteBody forShapeName:@"laser_1"];
    [laser setAnchorPoint:[[GB2ShapeCache sharedShapeCache] anchorPointForShape:@"laser_1"]];

    //pixeles recoridos/velocidad del move actual
    float timeAnim = 800/(60*move);
    [laser runAction:[CCSequence actions:
                      [CCMoveBy actionWithDuration:timeAnim position:ccp(-800, 0)],
                      [CCCallFuncN actionWithTarget:self selector:@selector(obstaclesDone:)],
                      nil]];    

我正在使用Physic Editor来创建形状。创建之后。游戏在60 fps下运行良好。但仅限于iPhone。在我的iMac中完美运行。

如何在不丢失fps的情况下创建它。也许在第二个处理器。还是另一个线程?

谢谢:)

1 个答案:

答案 0 :(得分:2)

您正在使用的纹理的大小和质量是多少?可能需要太多内存来创建精灵实例,这使得难以保持高帧速率不断改变这些精灵的位置。您也可以尝试使用Instruments进行分析,看看是什么占用了处理器周期。