Search code examples
ioscocos2d-iphonedeviceparticles

Cocos2D Particle bugged on Device


my particles are working fine in the simulator, but when I run the app with the device (iPhone 4S - iOS 6.1) it looks like some weird lasers (see the images below):

Particles on the Device: https://dl.dropboxusercontent.com/u/58621241/IMG_2287.PNG

Particles on Simulator: https://dl.dropboxusercontent.com/u/58621241/Screen%20Shot%202013-06-20%20at%2018.10.15.png

And here is the code where I create the particlesystem:

@implementation CCParticleMainDrill
-(id) init
{
    return [self initWithTotalParticles:100];
}

-(id) initWithTotalParticles:(NSUInteger) p
{
    if( (self=[super initWithTotalParticles:p]) ) {

        // _duration
        _duration = kCCParticleDurationInfinity;

        // Gravity Mode
        self.emitterMode = kCCParticleModeGravity;

        // Gravity Mode: gravity
        self.gravity = ccp(0,100);

        // Gravity Mode: speed of particles
        self.speed = 400;
        self.speedVar = 230;

        // Gravity Mode: radial
        self.radialAccel = 0;
        self.radialAccelVar = 0;

        // Gravity Mode: tagential
        self.tangentialAccel = 0;
        self.tangentialAccelVar = 0;
        ccBlendFunc ccbf = {GL_DST_COLOR,GL_DST_ALPHA};//{GL_SRC_ALPHA,GL_SRC_ALPHA_SATURATE};

        self.blendFunc = ccbf;

        // _angle
        _angle = 90;
        _angleVar = 60;

        // emitter position
        //CGSize winSize = [[CCDirector sharedDirector] winSize];
        self.positionType = kCCPositionTypeFree;
        self.position = ccp(0, 0);// ccp(winSize.width/2, winSize.height/2);
        self.posVar = ccp(3, 0);

        // _life of particles
        _life = 0.06;
        _lifeVar = 0.1;

        // size, in pixels
        _startSize = 5.0f;
        _startSizeVar = 1.0f;
        _endSize = 3.0f;
        _endSizeVar = 0.00f;

        // emits per second
        _emissionRate = _totalParticles/_life;

        // color of particles
        _startColor.r = 0.25f;
        _startColor.g = 0.14f;
        _startColor.b = 0.07f;
        _startColor.a = 1.0f;
        _startColorVar.r = 0.0f;
        _startColorVar.g = 0.0f;
        _startColorVar.b = 0.0f;
        _startColorVar.a = 0.0f;
        _endColor.r = 0.28f;
        _endColor.g = 0.19f;
        _endColor.b = 0.12f;
        _endColor.a = 0.30f;
        _endColorVar.r = 0.0f;
        _endColorVar.g = 0.0f;
        _endColorVar.b = 0.0f;
        _endColorVar.a = 0.0f;

        self.texture = [[CCTextureCache sharedTextureCache] addImage: @"particle.png"];

        // additive
        self.blendAdditive = YES;
    }

    return self;
}
@end

And this one when I add the Particles to the scene:

Armor = [CCSprite spriteWithFile:@"player.png"];
Armor.position = ccp(16, 16);    
[self addChild:Armor];
DrillParticles = [CCParticleMainDrill node];//[CCParticleSmoke node];     
[DrillParticles setPosition:ccp(Armor.boundingBox.size.width/2, 0)];
[DrillParticles setTotalParticles:0];
[Armor addChild:DrillParticles z:INT_MAX];

Solution

  • FIXED

    I was setting the number of particles to 0 as a way to pause particles animation. It works on the simulator but causes a bug on the device.

    Instead of changing the number of particles, I've created a function to change the _life and _lifeVar variables. Now it is working just fine. See the code below:

    Particle System Interface

    @interface CCParticleMainDrill : CCParticleSystemQuad
    {
        float _saved_life, _saved_lifeVar;
    }
    // needed for BridgeSupport
    -(void) SetActive:(BOOL) b;
    -(id) init;
    @end
    

    Particle System Implementation

    @implementation CCParticleMainDrill
    -(id) init
    {
        return [self initWithTotalParticles:100];
    }
    
    -(void) SetActive:(BOOL) b
    {
        if (b) {
    
            _life = _saved_life;
            _lifeVar = _saved_lifeVar;
    
        }
        else
        {
            _saved_life = _life;
            _saved_lifeVar = _lifeVar;
    
            _lifeVar = 0;
            _life = 0;
        }
    }
    
    -(id) initWithTotalParticles:(NSUInteger) p
    {
        if( (self=[super initWithTotalParticles:p]) ) {
    
            // _duration
            _duration = kCCParticleDurationInfinity;
    
            // Gravity Mode
            self.emitterMode = kCCParticleModeGravity;
    
            // Gravity Mode: gravity
            self.gravity = ccp(0,100);
    
            // Gravity Mode: speed of particles
            self.speed = 400;
            self.speedVar = 230;
    
            // Gravity Mode: radial
            self.radialAccel = 0;
            self.radialAccelVar = 0;
    
            // Gravity Mode: tagential
            self.tangentialAccel = 0;
            self.tangentialAccelVar = 0;
            ccBlendFunc ccbf = {GL_DST_COLOR,GL_DST_ALPHA};//{GL_SRC_ALPHA,GL_SRC_ALPHA_SATURATE};
    
            self.blendFunc = ccbf;
    
            // _angle
            _angle = 90;
            _angleVar = 60;
    
            // emitter position
            //CGSize winSize = [[CCDirector sharedDirector] winSize];
            self.positionType = kCCPositionTypeFree;
            self.position = ccp(0, 0);// ccp(winSize.width/2, winSize.height/2);
            self.posVar = ccp(3, 0);
    
            // _life of particles
            _life = 0.06;
            _lifeVar = 0.1;
    
            // size, in pixels
            _startSize = 5.0f;
            _startSizeVar = 1.0f;
            _endSize = 3.0f;
            _endSizeVar = 0.00f;
    
            // emits per second
            _emissionRate = _totalParticles/_life;
    
            // color of particles
            _startColor.r = 0.25f;
            _startColor.g = 0.14f;
            _startColor.b = 0.07f;
            _startColor.a = 1.0f;
            _startColorVar.r = 0.0f;
            _startColorVar.g = 0.0f;
            _startColorVar.b = 0.0f;
            _startColorVar.a = 0.0f;
            _endColor.r = 0.28f;
            _endColor.g = 0.19f;
            _endColor.b = 0.12f;
            _endColor.a = 0.30f;
            _endColorVar.r = 0.0f;
            _endColorVar.g = 0.0f;
            _endColorVar.b = 0.0f;
            _endColorVar.a = 0.0f;
    
            NSString *path = [[NSBundle mainBundle] pathForResource:@"particle" ofType:@"png"];
    
            self.texture = [[CCTextureCache sharedTextureCache] addImage: path];
    
            _saved_life = _life;
            _saved_lifeVar = _lifeVar;
    
            // additive
            self.blendAdditive = YES;
        }
    
        return self;
    }
    @end
    

    Adding Particle to Scene

    -(id) init
    {
            Armor = [CCSprite spriteWithFile:@"player.png"];
            Armor.position = ccp(16, 16);
    
            [self addChild:Armor];
    
            DrillParticles = [CCParticleMainDrill node];//[CCParticleSmoke node];//
    
            [DrillParticles setPosition:ccp(Armor.boundingBox.size.width/2, 0)];
            //[DrillParticles setTotalParticles:0]; //BUGGED CODE
            [Armor addChild:DrillParticles z:INT_MAX];
            [DrillParticles SetActive:NO];
    }
    -(void) StopAnimation
    {
        [DrillParticles SetActive:NO];
        //[DrillParticles setTotalParticles:0]; //  BUGGED CODE
    }
    
    -(void)StartAnimation
    {
        [DrillParticles SetActive:YES];
        //[DrillParticles setTotalParticles:100]; //  BUGGED CODE
    }