This is the fun part of developing games. You have to find ways how your character’s gonna die. On PoBo we had some triggers that we call bad_stones. First of all we got a stone that bursts out a fire spray when the player’s coming into contact with it.
After that we thought about other ways how the character can die when he collides with this triggers. We decided to take two more ways to die. A circular saw, that will cut the player into two halfs and a simple explosion that splats PoBo-blood across the screen .
Once you decided in your imagination how it should look like the rest will follow easily. And that’s what it looks like
Animating explosions with SKAnimations in Sprite Kit
For animations it’s more than just adding a fire for example to an object. The important thing is to think about how an animation reacts with the environment that simulates a real physical reaction for example. So lets see what we need for an explosion:
- A SKEmitterNode to show the explosion fire
use the Xcode built in emitter template spark for example and customize it to your needs - A SKEmitterNode to show the smoke that an explosion will cause
here also the smoke emitter template does a good job
- Some kind of a splat image that will cover the screen to make it look like the camera is full of the pieces that will be blown away from the explosion source
This can be simply drawn by your own with any common image software
- An animation that will shake the environment
This is a very important issue. To make an explosion look real the environment have to react like on a real explosion. To create this effect shake all the sprites with a small range of pixels from left to right and back again within a very small time range for example 0.1 seconds. We also scaled the whole background within a small time range up a very little and back again to make it look pulsating. - An explosion sound sample
If you don’t have your own sample library, some samples can be easily found for example at www.freesound.org
Just keep an eye on the usage with the copyrights for commercial products…
This is our method sourcecode for xcode using Sprite Kit to bring this points to life to simulate an explosion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
+(void) addExplosionToObject { // play an explosion sound SKAction *explosionSound = [SKAction playSoundFileNamed:@"explosion.mp3" waitForCompletion:TRUE]; [gameScene runAction:explosionSound]; //pulsate the background [[gameScene childNodeWithName:@"background"] runAction:[SKAction scaleTo:1.05 duration:0.05] completion:^{ [[gameScene childNodeWithName:@"background"] runAction:[SKAction scaleTo:1 duration:0.05]]; }]; //animate quake, shake the scene SKAction* moveX_1 = [SKAction moveBy:CGVectorMake(-7, 0) duration:0.05]; SKAction* moveX_2 = [SKAction moveBy:CGVectorMake(-10, 0) duration:0.05]; SKAction* moveX_3 = [SKAction moveBy:CGVectorMake(7, 0) duration:0.05]; SKAction* moveX_4 = [SKAction moveBy:CGVectorMake(10, 0) duration:0.05]; SKAction* moveY_1 = [SKAction moveBy:CGVectorMake(-0, -7) duration:0.05]; SKAction* moveY_2 = [SKAction moveBy:CGVectorMake(0, -10) duration:0.05]; SKAction* moveY_3 = [SKAction moveBy:CGVectorMake(0, 7) duration:0.05]; SKAction* moveY_4 = [SKAction moveBy:CGVectorMake(0, 10) duration:0.05]; SKAction* trembleX = [SKAction sequence:@[moveX_1, moveX_4, moveX_2, moveX_3]]; SKAction* trembleY = [SKAction sequence:@[moveY_1, moveY_4, moveY_2, moveY_3]]; for (SKNode *child in gameScene.children) { [child runAction:trembleX]; [child runAction:trembleY]; } // add a splat image over the screen SKSpriteNode *splat = [SKSpriteNode spriteNodeWithImageNamed:@"splat.png"]; splat.size = gameScene.size; [splat setScale:0]; splat.position = CGPointMake(gameScene.frame.size.width/2, gameScene.frame.size.height/2); [gameScene addChild:splat]; [splat runAction:[SKAction scaleTo:1 duration:0.1]]; // add the explosive fire emitter CGPoint pos = CGPointMake(collisionObject.position.x, collisionObject.position.y+ collisionObject.size.height/2); NSString *fireEmmitterPath = [[NSBundle mainBundle] pathForResource:@"spark" ofType:@"sks"]; SKEmitterNode *fireEmmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:fireEmmitterPath]; fireEmmitter.position = pos; [gameScene addChild: fireEmmitter]; [fireEmmitter runAction:[SKAction sequence:@[[SKAction waitForDuration:0.1], [SKAction scaleBy:1.5 duration:0.1], [SKAction runBlock:^ { fireEmmitter.particleBirthRate = 0; }]]]]; // add the smoke emitter NSString *smokeEmmitterPath = [[NSBundle mainBundle] pathForResource:@"smoke" ofType:@"sks"]; SKEmitterNode *smokeEmmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:smokeEmmitterPath]; smokeEmmitter.position = pos; [gameScene addChild: smokeEmmitter]; [smokeEmmitter runAction:[SKAction sequence:@[[SKAction waitForDuration:0.1], [SKAction scaleBy:1.2 duration:0.1], [SKAction runBlock:^ { smokeEmmitter.particleBirthRate = 0; }]]]]; } |
Great post! Glad to see you are using SpriteKit to get such great effects working in your game!
Could you kindly share the code for the Circular Saw death effect please? I’d be keen to see and learn how that works.