I've managed to obtain the effect, but performance sucks! =(
What I did was create a custom class for the emitter:
This is my class:
TrailParticles.h
#import "cocos2d.h"
@interface TrailParticlesSystem : CCNode
{
int totalParticles;
float life;
float lifeVar;
float timeToLive;
}
@property (nonatomic,readwrite,assign) float life;
@property (nonatomic,readwrite,assign) float lifeVar;
-(id) initWithTotalParticles:(int) numberOfParticles;
@end
TrailParticlesSystem.m
#define kParticlesStartingTag
#import "TrailParticlesSystem.h"
#import "EmittingParticle.h"
static ccColor4F fireworksColors[] = {
{1.0f, 0.0f, 0.0f, 1.0f}, //Red
{0.0f, 1.0f, 0.0f, 1.0f}, //Green
{0.0f, 0.0f, 1.0f, 1.0f}, //Blue
{1.0f, 1.0f, 0.0f, 1.0f}, //Yellow
{1.0f, 1.0f, 1.0f, 1.0f}, //White
{1.0f, 0.0f, 1.0f, 1.0f}, //Purple
{1.0f, 0.5f, 0.0f, 1.0f} //Orange
};
@implementation TrailParticlesSystem
@synthesize life, lifeVar;
- (id) initWithTotalParticles:(int) numberOfParticles
{
self.life = 1.0f;
self.lifeVar = 0.5;
ccColor4F startColor = fireworksColors[arc4random()%7];
ccColor4F endColor = fireworksColors[arc4random()%7];
if( (self=[super init]) )
{
timeToLive = life + lifeVar;
totalParticles = numberOfParticles;
for (int i =0; i< totalParticles; i++)
{
EmittingParticle *particle = [EmittingParticle node];
particle.startColor = startColor;
particle.endColor = endColor;
particle.life = self.life;
particle.lifeVar = self.lifeVar;
particle.velocity = ccp(CCRANDOM_MINUS1_1()*50,CCRANDOM_MINUS1_1()*50);
particle.acceleration = ccp (0.0, -40.0);
[particle setPosition:self.position];
[self addChild:particle z:0 tag:kParticlesStartingTag + i];
}
[self scheduleUpdate];
}
return self;
}
- (void)update:(ccTime)dt
{
EmittingParticle *particle;
CCARRAY_FOREACH([self children], particle)
[particle updateSystem:dt];
timeToLive -= dt;
if (timeToLive <= 0.0)
[self removeFromParentAndCleanup:YES];
}
- (void)dealloc
{
[super dealloc];
}
@end
I then add it to my scene like this:
TrailParticlesSystem *emitter = [[TrailParticlesSystem alloc] initWithTotalParticles:40];
emitter.position = pos;
[self addChild:emitter];
Is there a way to preallocate all the emitters in the particle system or copy them without allocing?