Playing a sound when game over

Discussion in 'Mac Programming' started by julthebaber, Feb 2, 2014.

  1. julthebaber, Feb 2, 2014
    Last edited by a moderator: Feb 2, 2014

    julthebaber macrumors newbie

    Joined:
    Feb 2, 2014
    #1
    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:
    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];
        }
        
    }
    
    Please help me i tried for hours and nothing worked!
     
  2. lee1210, Feb 2, 2014
    Last edited: Feb 2, 2014

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Code:
    -(BOOL) determineCollision {
    
      NSArray *arrayOfCollisionRects = [NSArray arrayWithObjects: 
        [NSValue valueWithCGRect:Obstacle.frame],
        [NSValue valueWithCGRect:Obstacle2.frame],
        [NSValue valueWithCGRect:Bottom1.frame],
        [NSValue valueWithCGRect:Bottom2.frame],
        [NSValue valueWithCGRect:Bottom3.frame],
        [NSValue valueWithCGRect:Bottom4.frame],
        [NSValue valueWithCGRect:Bottom5.frame],
        [NSValue valueWithCGRect:Bottom6.frame],
        [NSValue valueWithCGRect:Bottom7.frame],
        [NSValue valueWithCGRect:Top1.frame],
        [NSValue valueWithCGRect:Top2.frame],
        [NSValue valueWithCGRect:Top3.frame],
        [NSValue valueWithCGRect:Top4.frame],
        [NSValue valueWithCGRect:Top5.frame],
        [NSValue valueWithCGRect:Top6.frame],
        [NSValue valueWithCGRect:Top7.frame],
        nil];
    
      BOOL hasCollided = NO;
      for (NSValue *singleObject : arrayOfCollisionRects) {
        if(CGRectIntersectsRect(Heli.frame, [singleObject CGRectValue]) {
          hasCollided = YES;
          break;
        }
      }
      hasCollided |= (Heli.center.y > 264);
      hasCollided |= (Heli.center.y < 29);
      if(hasCollided) {
        [self playCollisionSound];
        [self endGame];
      }
      return hasCollided;
    }
    
    Move the playback code into a playCollisionSound method. Rename EndGame to endGame. Potentially move 264 and 29 into constants. If the Top and Bottom and such are NSObjects put those directly into the array rather than wrapping the CGRects, then just grab the frame in the loop.

    Not sure where you use this, but you could just call the playing of the sound and endGame from there after checking this return value instead of doing those things inside this method.

    -Lee
     

Share This Page