For some reason, my frame rate drops from 60 to 30 just by adding a static background. If I add a repeating background, it then drops to 20.
Here is my implementation:
//adding static bg
staticBG = [CCSprite spriteWithFile:@"staticBG.png"];
staticBG.position = ccp(240,160);
[self addChild:staticBG z:-10];
// create the repeating backround/map layer and add it to the scene
int numRepeat = 2000; //number of times background will repeat
int numPixels = 1024;
CGSize screenSize = [[CCDirector sharedDirector] winSize];
background = [CCSprite spriteWithFile:@"bg.png" rect:CGRectMake(0, 0, numPixels * numRepeat, 512)];
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[background.texture setTexParameters:¶ms];
[background setPosition:ccp(0, screenSize.height/2)]; // center sprite;
[self addChild:background z:-9];
Here is my animation approach within a mainGameLoop which is triggered by a scheduler:
`int bgMoveRate = 250;
int interimBGMoveRate = 5;
CGPoint newBGLocation = ccp(background.position.x - distance/bgMoveRate * -cosf(touchAngle), background.position.y);
background.position = newBGLocation;
CGPoint newInterimBGLocation = ccp(interimBG.position.x - distance/interimBGMoveRate * -cosf(touchAngle), interimBG.position.y);
interimBG.position = newInterimBGLocation;`
I'm using a joypad to make the elements move:
- (void)ccTouchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
NSSet *allTouches = [event allTouches];
for (UITouch *touch in allTouches) {
if ([touch hash] == joypadTouchHash && joypadMoving) {
// Get the point where the player has touched the screen
CGPoint point = [touch locationInView:[touch view]];
CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:point];
// Calculate the angle of the touch from the center of the joypad
float dx = (float)joypadBG.position.x - (float)convertedPoint.x;
float dy = (float)joypadBG.position.y - (float)convertedPoint.y;
distance = sqrtf((joypadBG.position.x - convertedPoint.x) * (joypadBG.position.x - convertedPoint.x) +
(joypadBG.position.y - convertedPoint.y) * (joypadBG.position.y - convertedPoint.y));
// Calculate the angle of the players touch from the center of the joypad
touchAngle = atan2(dy, dx);
// If the players finger is outside of the joypad, make sure the joypad cap is drawn at the joypad edge.
if (distance > joypadMaxRadius) {
joypadCap.position = ccp(joypadBG.position.x - cosf(touchAngle) * joypadMaxRadius,
joypadBG.position.y - sinf(touchAngle) * joypadMaxRadius);
} else {
joypadCap.position = convertedPoint;
}
}
}
}
My backgrounds are less than 200kb in size. I don't getting any warnings at compile time.
I really wanted to use Lam's CCTransformTexture class because I can set the repeat to forever, but I'm having trouble implementing it into my mainGameLoop.
Any ideas?