Search code examples
ioscocos2d-iphoneparticles

CCParticleSystem and iPhone "bug" or limitation?


I seemed to have run into a strange problem with the CCParticleSystem and the iPhone.

I have a laser that is being shot across the screen from left to right. I added a particle effect to give the laser more of a railgun look to it. I used the "emmas sharing" particle effect from Particle Designer.

Here is the code to send the laser across the screen:

-(void)fireLaserCannonAddon
{
    if( _ship == nil || _ship.dead ) return;
    CGSize winSize = [CCDirector sharedDirector].winSize;
    shipLaserCannon = [_laserCannonArray nextSprite];
    [shipLaserCannon stopAllActions];
    shipLaserCannon.position = ccpAdd(_ship.position, ccp(shipLaserCannon.contentSize.width / 2, -shipLaserCannon.contentSize.height));

    [shipLaserCannon revive];

    CCParticleSystemQuad *laserEffect = [_laserEffect nextParticleSystem];
    [laserEffect resetSystem];

    [shipLaserCannon runAction:[CCSequence actions:
                                [CCMoveBy actionWithDuration:0.5 position:ccp(winSize.width, 0)],
                                [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)],
                                [CCCallFunc actionWithTarget:self selector:@selector(endLaserEffects)], nil]];
}

And the code to set the particle system effect to the laser's position:

-(void)updateLaserEffects:(ccTime)dt
{
    for( CCParticleSystemQuad *laserEffect in _laserEffect.array )
    {
        laserEffect.position = shipLaserCannon.position;
    }
}

-(void)endLaserEffects
{
    for( CCParticleSystemQuad *laserEffect in _laserEffect.array )
    {
        [laserEffect stopSystem];
    }
}

If you open up the "emmas sharing" effect in Particle Designer, the effect is the same as when you click and drag across the screen. This works perfectly on the iPad and iPad simulator, however on my iPhone 3GS / iPhone (SD and retina) simulator, the emitted particles seem to be "carried" with the laser. It's not as identical as setting the PositionType to kCCPositionTypeGrouped (the emitted particles stay in that circle shape), but kind of a mix between it being kCCPositionTypeGrouped and kCCPositionTypeFree. The particles are emitting off the laser, but also being a dragged a bit behind the laser instead of staying where it was emitted like on the Particle Designer simulator and regular iPad. It looks as if the laser is creating its own layer with the particle effect on it with the "layer" lagging behind it.

I thought that maybe the laser was moving too fast, but even when slowed down, it had the same effect.

This "bug" also creates another small problem, since it's being "carried" with the laser, when the laser is off the screen and then taken out, the remnants of the last emitting particles are visible on the bottom left of the screen, since I'm sure its because the emitted particles are still following the position.x of the laser (which it shouldn't be doing, only the base of it is supposed to) and since the laser is gone, it defaults to it's default set position. However, I do not have this problem on the iPad / iPad simulator.

BTW, this wasn't just limited to only the "emma sharing" particle effect, it seems to do the same for all the other effects.

Has anyone else ever had similar issues with using CCParticleSystems on a moving object for the iPhone?

Any helpful input is greatly appreciated!


Solution

  • OK, so after some messing around, I found out what was causing all this.

    I originally had the CCParticleSystem set to 1.0 (original scale) for the iPad and 0.5 for the iphone. I changed the scale for the iPhone to 1.0 and everything worked like it should..just a lot bigger, but it worked. I really didn't want to have two different particle effects for the same effect just because of screen size, so I figured I'll just scale up to 2.0 for the iPad while leaving 1.0 on the iPhone. Low and behold, now the iPad suffered the same weird looking effect as I did on the iPhone, but much more extreme.

    Looks like I don't have much of a choice now, but to have two different files for the same effect, but I'm relieved I found out what was causing this and can save a few hairs from leaving prematurely.

    I think scaling an effect is fine as long as it's not following an object dynamically, like in my case.

    I don't know if this would be considered as a bug or not, since I'm sure it's a math thing that cocos2d is using and scaling it affects it.

    TLDR::Scaling up/down a particle effect will cause this weird effect when it is following an object's position. Don't re-scale particle effects that are following an object's position dynamically. If it's just in one spot, then it's fine.