views:

616

answers:

4

Hi,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??

+1  A: 

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];
arul
A: 

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.

Matt Long
Cocos already supports Layers through the Layer class.
arul
+1  A: 

Hello friend, 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.
Mr.Gando
A: 

I would try asking this also over at iPhoneGameDev, it's a StackOverflow like site specifically for iPhone game development - you might find someone there who knows Cocos2D well and can suggest an approach.

Kendall Helmstetter Gelner