English 中文(简体)
Cocos2d - Shooting Game Problem
原标题:

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

最佳回答

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.
问题回答

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];

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.

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签