2

everyone, I made a shooting game just like 1942(classic shooting game),but here's the question, FPS drops to 5~6 when the fighter shoot out a straight line of bullet,about 7~8 bullets in the screen,Actually, only 8 bullets of player will appear in games, when bullets fly out of the screen they would reset to the fighter's center and become invisible,all bullets are individual sprites, just like:

Sprites * bullet1 = [Sprite spriteWithFile:@"bullet.png"];

........

Sprites * bullet8 = [Sprite spriteWithFile:@"bullet.png"];

Is there any method can store a lots of bullets instead of defining them one by one?? Because enemies may fire too, I can imagine the FPS may get closer to zero.....

Can anyone help me??

4 Answers 4

3

you could do something like have a "bullet pool" in there, as you are using cocos, what I would do is to allocate lot's of bullets when I start the game. Check how many bullets do you need approximately, pre allocate those.

When an Entity in your game needs to fire a bullet, it just asks for a bullet to this pool, you give the properties to the "pre allocated" bullet, the bullet appears on the screen, and when it impacts/disappear you then return that bullet to your bullet pool.

if you need some code:

/*You pre-allocate your bullets.*/
for(int i = 0; i < MAX_BULLETS; i++)
{
    Bullet *aBullet = [[Bullet alloc] init];
    [bulletsArray addObject:aBullet];

    [aBullet release];
}

//Then in game when you fire:

Bullet *aBullet = [PoolManager bulletWithSprite:myBulletSprite]; // Where myBulletSprite is PRE allocated and you don't allocate sprites in Game.
1

You can store the sprites in an array:

NSMutableArray * bulletsBuilder = [[NSArray alloc] init];

for(int i = 0; i < MAX_NUMBER_OF_SPRITES; ++i) {
    [bullets addObject:[Sprite spriteWithFile:@"bullet.png"]];
}

NSArray * bullets = [NSArray arrayWithArray:bulletsBuilder];
[bulletsBuilder release];

And access them later using their identifier:

Sprite * spr = [bullets objectAtIndex:spriteIndex];
0

What's a problem? Use sprite batching! It's kind of special layer that allow you to speed up the rendering of the multiple sprites.

  1. CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];
    • Unless you're not Texture Atlas you cannot use different sprites to render them.
  2. Then load one sprite and add to the batch itself as many sprites as you need. for(;i>10;){[batch addSprite:sprite];}
  3. Add to self.
-1

Core Animation layers are lightweight and should help your performance. You can load the image once and then load the bullets image into the contents of an array of CALayers. Something like this:

// bulletImage is an instance variable
bulletImage = [UIImage imageNamed:@"bullet.png"];

bulletLayers = [[NSMutableArray alloc] init];
for (i = 0; i < 9; ++i)
{
    CALayer *bulletLayer = [CALayer layer];
    [bulletLayer setBounds:[bulletImage bounds]];
    [bulletLayer setPosition:gunBarrelOrigin];
    [bulletLayer setContents:(id)[bulletImage CGImage]];
    [bulletLayers addObject:bulletLayer];
}

// Use the array of layers.
// ...

You don't specify whether you are using views or layers in your Sprite class, but if you are loading the same image multiple times, you're definitely creating unnecessary overhead.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.