I want to create a particle effect when my player sprite collide with my star sprite. How can I add a particleSystem in the sprite and not in a image? Also, I wish if it could wait for the animation for some miliseconds, and then remove the star sprite.

This is my code:

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair starCollision:
    (CCNode *)star playerCollision:(CCNode *)_player {

    //update score
    score += 1;
    [scorelabel setString: [NSString stringWithFormat:@"score: %d",score]];
    NSLog(@"score %d", score);

    CCParticleSystem *particleSystem = [[CCParticleSpiral alloc] initWithTotalParticles:1000];

    [self addChild:particleSystem];
    //[particleSystem release];
    //[particleSystem setTexture:[[CCTextureCache  sharedTextureCache] addImage:@"star.png"];

    //remove stars
    [star removeFromParent];
    return YES;

I am not seeing where you are positioning the particle system, only that you are adding it to "self". I assume you want the particle system to be more or less at the same position as the star sprite.

Assuming "self" is a layer, you need to position the particle system using the position of the star sprite, and offset the position where needed.

particleSystem.position = star.position;

Or you could just add the particle system as a child of the star sprite, and offset the position where needed.

[star addChild:particleSystem];

With regard to waiting some period of time before removing the star sprite, you could use actions to do this. The first action would be a delay, and the second action would then call a method to remove the star:

id delayAction = [CCDelayTime actionWithDuration:0.1];
CCAction * removeStar = [CCCallFuncN actionWithTarget:self selector:@selector(removeStar)];
[star runAction:[CCSequence actions: delayAction, removeStar, nil]];

- (void) removeStar {
    [star removeFromParent];

Your question was not very clear, so this was the best I could do with the information provided. Also, it does not appear that you are properly preparing the particle system. But, since you did not specifically ask about that I am assuming your code just lists the basic information about this.

I hope this helps.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.