I am having a slow fps problem as soon as my gamelayer is initialized. While with other layers like Menulayer fps is 55-60 fps but as soon as Gamelayer is loaded fps comes down to 7-8 fps. I am adding 19 objects which are initialized as following. its a subclass of Sprite(Cocos2D).
As soon as the first collision occurs with any object out of 19, or among themselves or with another object the fps comes to 55-60 fps. And it never goes down during the full game play. I am unable to find out what is the problem and why this is happening only in the beginning?
Code: Select all
-(id ) initWithImageFile:(NSString *)imageName inSpace:(cpSpace*)inSpace
{
self = [super initWithFile:imageName];
[self setPosition:ccp(0, 0)];
image = [UIImage imageNamed:imageName];
radius = image.size.width / 2 - OFFSET;
height = image.size.height - (2 * OFFSET);
width = image.size.width - (2 * OFFSET);
isCollided = NO;
int num_vertices = 4;
cpVect verts[4]= {
cpv(radius * -1, radius * -1),
cpv(radius * -1, radius),
cpv(radius, radius),
cpv(radius, radius * -1)
};
body = cpBodyNew(1.0, cpMomentForPoly(1.0, num_vertices, verts, cpvzero));
body->p = cpv([self position].x, [self position].y);
body->v = cpvzero;
body->f = cpvzero;
cpSpaceAddBody(inSpace, body);
shape = cpCircleShapeNew(body, radius+0.5, cpvzero);
shape -> data = self;
shape -> u = 0.4f;
shape -> e = 0.7f;
cpSpaceAddShape(inSpace, shape);
return self;
}
When I am loading these objects without adding body and shapes to space
Code: Select all
//cpSpaceAddBody(inSpace, body);
//cpSpaceAddShape(inSpace, shape);
the fps is 55-60 but as soon as I add the body and shapes to space the fps comes down to 7-8.
My step function is as following
Code: Select all
-(void) step: (ccTime) delta
{
int steps = 2;
cpFloat dt = 1.0/60.0/2.0f;
for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
cpSpaceHashEach(space->activeShapes, &eachShape, self);
cpSpaceHashEach(space->staticShapes, &eachShape, self);
}
Code: Select all
cpSpaceResizeStaticHash(space, 100, 500);
cpSpaceResizeActiveHash(space, 100, 500);
Thanks in advance.
Nikesh