Search code examples
iphoneioscocos2d-iphoneparticles

cocos2d game, ccparticle system texture messed up


my game use several particle systems. every time i use the first particle system, stars.plist for example, the next particle to be displayed will use the texture of stars.plist. (so if next particle is rain, then will be a rain of stars :( I use [ARCH_OPTIMAL_PARTICLE_SYSTEM particleWithFile: @""], like:

CCParticleSystem* rain = [ARCH_OPTIMAL_PARTICLE_SYSTEM particleWithFile:@"rain.plist"];
[self addChild:rain z:-1];

and before initializing the rain, i have a call to preload the particle simply using:

[ARCH_OPTIMAL_PARTICLE_SYSTEM particleWithFile:@"rain.plist"];

then if my next particle to play is meteor.plist, the texture used is still the rain.plist's texture.

i use the same way to display meteor:

    NSString* fn = [NSString stringWithFormat:@"meteor%i.plist", random];
    CCParticleSystem* meteor = [ARCH_OPTIMAL_PARTICLE_SYSTEM particleWithFile:fn];

I've tried CCParticleQuad and CCParticlePoint, in both 3gs and iPhone 4 and simulator, but still the same problem.


Solution

  • If the texture data is embedded within the .plist file, cocos2d will extract that data and store it as a texture internally. The texture is stored with the texture filename as key, so if you have two particle-systems that use the same texture-filename internally, then the texture that was loaded first will be applied for all subsequent uses of the same texture filename!

    An easy way to check this is looking at your .plist file and search the textureFileName key. This key should be different for each texture. Since the image-data is embedded in your .plist file, you can change this value to anything you want.. eg. starparticle.png for your stars, rainparticle.png for the rain, etc. Just use something unique for every unique texture.

    If that fails, you could still apply the texture manually, by loading it from a file like this:

    [rain setTexture: [[CCTextureCache sharedTextureCache] addImage:"particleTexture.png"]];