Game Development Reference
In-Depth Information
Let's check the course of the action:
1.
Add the following properties to
ERGPlayer.h
:
@property (strong, nonatomic) NSMutableArray *shieldOnFrames;
@property (strong, nonatomic) NSMutableArray *shieldOffFrames;
@property (strong, nonatomic) SKSpriteNode *shield;
@property (assign, nonatomic) BOOL shielded;
2. The first two properties are arrays for animation frames, the
shield
node is
added to a character sprite in order to show shield animations (we can't show
it on the character itself as it will disappear), and
shielded
is a state variable
so that other nodes can find out if we are shielded or not.
3.
The next step is to create a
shield
node in the
init
method of
EGPlayer
:
self.shield = [[SKSpriteNode alloc] init];
self.shield.blendMode = SKBlendModeAdd;
[self addChild:self.shield];
Here, we change
blendMode
to
add
since it results in a better visual effect.
Blending modes
A blending mode is the way how different pixels are added
together. By default,
SKBlendModeAlpha
is used. It uses an
alpha value of the pixel to determine which pixels and what
percent of color of each pixel are visible, and which are not.
Other blending modes are used if you need to stimulate light
(the additive blending mode) or increase the brightness and
color, or completely overlay one layer by another. The list of
available blending modes can be found in the
SKBlendMode
class reference.
4.
The next thing to do is add another custom setter for the
shielded
variable,
where we will handle all animations:
- (void) setShielded:(BOOL)shielded
{
if (shielded) {
if (![self.shield actionForKey:@"shieldOn"]) {
[self.shield runAction:[SKAction
repeatActionForever:[SKAction animateWithTextures:self.
shieldOnFrames timePerFrame:0.1 resize:YES restore:NO]]
withKey:@"shieldOn"];
}
} else if (_shielded) {
[self blinkRed];