Tutorial details

Performing SKEmitterNode animation on iOS | App Code for Sale | Preview

Performing SKEmitterNode animation on iOS | iOS Tutorial

This tutorial is based on the component Car Chase - Highway
Download

Overview PAGE TOP

Particle emitters are used to create special effects in your game (explosions, blood splats, smoke, etc.). In this tutorial we'll show you how to use SKEmitterNode as property of sprite object.

Particle emitters PAGE TOP

Particle emitters are used to create special effects in your game (explosions, blood splats, smoke, etc.). If you would like to create particle emitter first thing you'll have to create is SKS particle file which is used by SKEmitterNode to show desired effect. It is common to use the SKEmitterNode as property of sprite object and in this tutorial we’ll show you how to use it in that way.

The method to start emitter will be added to SKSpriteNode and it will work in a similar way to how runAction method works. We'll define it with duration and completion block to make it easier for use. In your SKSpriteNode class header file define method like follows:

- (void)runEmitterAction:(SKEmitterNode *)emitter withDuration:(double)duration onCompletion:(void (^)())completion;

 Whole method that you'll add to your main class is defined here:



- (void) runEmitterAction:(SKEmitterNode *)emitter withDuration:(double)duration onCompletion:(void (^)())completion {

    if (emitter.scene) {
        [emitter removeFromParent];
    }
    [self addChild:emitter];
    [emitter resetSimulation];
    SKAction *delay =
        [SKAction moveByX:0.0f y:0.0f duration:duration];
    [self runAction:delay completion:^{
        completion();
    }];
}
In order to use this method you should first define SKEmitterNode as property:



@property (readonly, nonatomic) SKEmitterNode *emitterExplosion; You should initialise SKEmitterNode in initWithImageName method:

NSString *explosionPath = [[NSBundle mainBundle] pathForResource:@"Explosion" ofType:@"sks"]; _emitterExplosion = [NSKeyedUnarchiver unarchiveObjectWithFile:explosionPath]; With code you wrote you defined sprite that can execute our method in following way:

[firstCar runEmitterAction:firstCar.emitterExplosion withDuration:1.6 onCompletion:^{ [firstCar removeFromParent]; }]; Method runEmitterAction will be executed with emitter you want and with duration of 1.6 seconds, after completion sprite is removed from its parent object.

Practical use PAGE TOP

One of games on our marketplace is using this method to show explosion effects when cars collide.




On the left image you can see a race game and on the right image you can see the explosion effect that happens in this game when cars collide. When collision is detected both cars will perform emitter method with desired effect and duration, the main car will stay on road (player looses life) and the other car will be removed from gameplay.

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.