If you need a torch in your game, you can do that easily with an emitter. The built in emitter editor in XCode has even a fire template. If you use this fire .sks file you can easily put that in your game scene as a SKEmitterNode. But a fire or a torch need also some ambient light and effects to look suitable in your game.
The ambient light
Let’s first add a ambient background light around the fire emitter. This is not so easy if you want to do it without the forthcoming SKLightNode in iOS8 or customize the ambient shine. The trick is to add a sprite node with a transparent gradient with the light color like this image
The effect is that this will also enlight dark backgrounds in your scene.
The flickering animation
In detail you can see that some rays are added to the shining image above. A simple flickering effect can be done with animating this sprite. The animation should contain some scaling for the pulsating effect and some minimal rotation for the flickering effect to make it look like in the video above.
Here is the complete class :
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 |
// // WWLightFire.m // PoBo // // Created by zinne on 23.01.14. // Copyright (c) 2014 waveworks. All rights reserved. // #import "WWLightFire.h" @implementation WWLightFire +(void) addFire:(CGPoint)position game:(SKScene *)game { SKSpriteNode* shine = [SKSpriteNode spriteNodeWithImageNamed:@"shine"]; shine.position = CGPointMake(position.x, position.y + 50); shine.alpha = 0.3; [shine setScale:0.6]; [game addChild:shine]; SKAction* fadeOut =[SKAction fadeAlphaTo:0.28 duration:0.08]; SKAction* fadeIn =[SKAction fadeAlphaTo:0.3 duration:0.08]; SKAction* scaleSmall =[SKAction scaleTo:0.6 duration:0.03]; SKAction* scaleBig =[SKAction scaleTo:0.609 duration:0.03]; SKAction* rotateLeft =[SKAction rotateToAngle: 0 duration:0.03]; SKAction* rotateRight =[SKAction rotateToAngle:0.01 duration:0.03]; SKAction* wait = [SKAction waitForDuration:0.1 withRange:0.3]; SKAction* seq = [SKAction sequence:@[rotateLeft, fadeOut, scaleSmall, wait, rotateRight, fadeIn, scaleBig]]; [shine runAction:[SKAction repeatActionForever:seq] ]; //FireBody for collision detection SKSpriteNode *fireContainer = [SKSpriteNode spriteNodeWithImageNamed:@"dummypixel_transparent.png"]; fireContainer.name = @"lightfireContainer"; fireContainer.position = CGPointMake(position.x, position.y+35); fireContainer.size = CGSizeMake(20, 70); fireContainer.alpha = 0.5; fireContainer.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:fireContainer.size]; fireContainer.physicsBody.mass = 0.0000000000001; fireContainer.physicsBody.affectedByGravity = FALSE; //fireContainer.physicsBody.categoryBitMask = kNilOptions; fireContainer.physicsBody.collisionBitMask = kNilOptions; [game addChild:fireContainer]; SKSpriteNode* firebowl = [SKSpriteNode spriteNodeWithImageNamed:@"lightfirecontainer.png"]; firebowl.position = CGPointMake(position.x, position.y + 5); [firebowl setScale:0.4]; firebowl.zPosition = 1; [game addChild:firebowl]; SKSpriteNode* coal = [SKSpriteNode spriteNodeWithImageNamed:@"coal"]; coal.position = CGPointMake(position.x, position.y - 12); [coal setScale:0.4]; [game addChild:coal]; //load the fire emitter NSString *fireEmmitterPath = [[NSBundle mainBundle] pathForResource:@"fire" ofType:@"sks"]; SKEmitterNode *fireEmmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:fireEmmitterPath]; fireEmmitter.position = position; fireEmmitter.name = @"lightfire"; [game addChild: fireEmmitter]; } @end |