Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ParticleSystems in cocos2d

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?

like image 891
Mattias Akerman Avatar asked Jul 16 '09 15:07

Mattias Akerman


2 Answers

refer ccparticleexamples.m in your cocos2d library.

It will help for show the different types of particle emitter.

like image 183
Sri Avatar answered Sep 20 '22 18:09

Sri


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.

like image 30
GloryFish Avatar answered Sep 18 '22 18:09

GloryFish