I am doing a game, and I got only 1 final problem. Its that when the game create an enemy the FPS slow to 40 or 20. Depends if it creates 1 or 2 enemies.
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]];
I am using Physic Editor, to create the shape. After it create. The game works fine in 60 fps. But only in the iPhone. In my iMac works perfectly.
What could do I do to create it without loosing fps. Maybe in the second processor. Or another thread?
Thanks 🙂
What is the size and quality of the textures you are using? It could be that too much memory is required to create sprite instances making it difficult to keep a high frame rate constantly changing the positions of those sprites. You could also try profiling with Instruments to see what is eating up your processor cycles.