Hey everyone, this is my first post on here! I've been learning objective-C for the past month. It's my first programming language and so far i love learning it. I've followed a tutorial on how to make an helicopter game (avoiding obstacles). I want to add a background music and a explosion sound when the helicopter hits an obstacle. I manage to put the background music but using this code in the viewDidLoad method:
Please help me i tried for hours and nothing worked!
Code:
NSString *music = [[NSBundle mainBundle]pathForResource:@"BackgroundMusic" ofType:@"mp3"];
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
avPlayer.delegate = self;
avPlayer.numberOfLoops = -1;
[avPlayer play];
I then added this code so when the helicopter crashes the music stops. I added it in the EndGame method:
-(void)EndGame{
if ([avPlayer isPlaying]) {
[avPlayer pause];
}
if (ScoreNumber > HighScore) {
HighScore = ScoreNumber;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:@"HighScoreSaved"];
}
Heli.hidden = YES;
[timer invalidate];
[Scorer invalidate];
[self performSelector:@selector(NewGame) withObject:Nil afterDelay:5];
}
Now i'm trying to add a explosion sound in the Collision method and the code looks like this BUT when i try it, it makes the background music stops! It works, when the helicopter hits an obstacle the explosion plays but the background music doesnt play!
-(void)Collision{
if (CGRectIntersectsRect(Heli.frame, Obstacle.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Obstacle2.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom1.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom2.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom3.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom4.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom5.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom6.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom7.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top1.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top2.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top3.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top4.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top5.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top6.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top7.frame)) {
[self EndGame];
}
if (Heli.center.y > 264) {
[self EndGame];
}
if (Heli.center.y < 29) {
[self EndGame];
}
NSString *music = [[NSBundle mainBundle]pathForResource:@"Explosion" ofType:@"mp3"];
avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
avPlayer.delegate = self;
avPlayer.numberOfLoops = 1;
[avPlayer play];
[avPlayer setVolume:0.1];
}
}
Last edited by a moderator: