Game Development Reference
In-Depth Information
Why haven't we used this action to play background music? It is used for playing
short sounds, as somehow removing this action does nothing. It is unclear if it is a
bug or a feature, but it is present in the previous version of Sprite Kit. The music
just keeps on playing.
As we handle shield effects in Player.m , it is a good place to add playback
sound. We will change the setShielded: method by adding music actions
to it. Replace your method with the following:
- (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"];
SKAction *musicAction = [SKAction playSoundFileNamed:@"shi
eldCharged.wav" waitForCompletion:NO];
[self runAction:musicAction];
}
} else if (_shielded) {
[self blinkRed];
[self.shield removeActionForKey:@"shieldOn"];
[self.shield runAction:[SKAction animateWithTextures:self.
shieldOffFrames timePerFrame:0.15 resize:YES restore:NO]
withKey:@"shieldOff"];
SKAction *musicAction = [SKAction playSoundFileNamed:@"shieldS
mashed.wav" waitForCompletion:NO];
[self runAction:musicAction];
}
_shielded = shielded;
}
As before, it handled the shield logic, but there is a little bit of playback sound
added to it.
Build and run the project; it should run and you should have background music
and sound effects fully working.
There is one more issue with our code. If you receive a call or exit from the
application, it will crash. This happens due to the fact that we need to close
AVAudioSession before our application resigns active. This class is used
underneath SKAction to play sounds.
 
Search WWH ::




Custom Search