views:

638

answers:

3

I wonder about what road I should go with ParticleSystem. In this particular case I want to create 1-20 small explosions at the same time but with different positions. Right now I'm creating a new ParticleSystem for each explosion and then release it, but of course this is very punishing to the performance.

My question is: Is there a way to create one ParticleSystem with multiple emitting sources. If not should I create an array of ParticleSystem in init and then use a free one when an explosion is needed? Or is there another approach I haven't thought of?

A: 

Well, since no help was to be found I solved it in a way I thought was best. I made an array of Partlesystems.

When I needed an explosion at several points at once I basicly did this:

ParticleSystem *ps = [EnemyExplosions objectAtIndex:EnemyExplosionPointer++]; ps.position = ExpPoint; [ps resetSystem];

Mattias Akerman
A: 

I don't know if you can create multiple sources for a single ParticleSystem, but your second solution (creating an array of ParticleSystem) is a good general solution for solving this kind of problem. As long as you can reinitialise the ParticleSystem, you will avoid any other overhead such as instantiation costs and memory fragmentation.

Matt Curtis
A: 

I use a single particle system for each type of effect. So, one for a fire effect, one for a sparkle, one for an explosion, etc.

I use a category to add [enable] and [disable] methods to the base Cocos2D particle systems which looks like this:

ParticleEnhancement.h

#include "cocos2d.h"

@interface CCParticleSystem (particleEnhancement)
-(void)enable;
-(void)disable;
@end

ParticleEnhancement.m

#import "ParticleEnhancement.h"

@implementation CCParticleSystem (particleEnhancement)

-(void)enable {
  active = YES;
  elapsed = 0.0f;
}

-(void)disable {
  active = NO;
}

@end

Then when I want to trigger it I just set the position and call enable. The particle system will spawn a bunch of particles based on the initialization settings. The system can be triggered multiple times in different positions and previously generated particles will behave properly.

The main thing to consider is that you will need a larger particle budget to account for multiple instances of a given effect using a single system.

Also, this works for "triggered" effects, like an explosion but may not work so well for a long running effect like a smoke trail, I haven't really tested that.

I like doing it this way because it means I have fewer particle systems to wrangle and I don't have to deal with setting up a pool of particle systems. The particle system itself does a good job of handling a pool of particles, no need to replicate that again on the system level.

This technique is being used in my app TCG Counter for all of the particle effects.

GloryFish