This discussion is archived
11 Replies Latest reply: Jan 15, 2010 7:49 AM by 800594 RSS

Problems with Applet sounds - non-static method cannot be refferenced...

843853 Newbie
Currently Being Moderated
Hi, Ive created a Java Pacman Game Applet and am trying to add sounds.
I have declared in my static method updateWorld():
AudioClip munch = getAudioClip(getDocumentBase(), "sounds/gulp.wav");
Then included:
if(pacRec.intersects(cellRect)){
board[count1][count2]=2;
score=score+10;
if (score==1450)
win();
munch.play();
But im getting 2 error messages:
non static method getDocumentBase cannot be refferenced from static context
non static method getAudioClip(java.net.URL,java.lang.string) cannot be refferenced from static context

I cant make the updateWorld method non static so is there a way around this to fix the problem so that pacman can munch his food with sound???

Here are bigger sections of the code case you need it....
public static void updateWorld(){
             int count1 = 0;
             int count2 = 0;
             int xbound = 32;
             int ybound = 32;
            Rectangle pacRec = pac.getBoundingBox();
          
          
          AudioClip munch = getAudioClip(getDocumentBase(), "sounds/gulp.wav");
          
          for(count1=0; count1 < 19; count1++){
               for(count2=0; count2 < 19; count2++){     
                    switch (board[count1][count2]){
                         case 0: 
                         {
                              Rectangle cellRect = new Rectangle(xbound,ybound,32,32);
                              if(pacRec.intersects(cellRect)){
                                   board[count1][count2]=2;
                                   score=score+10;
                                   if (score==1450)
                                        win();
                                   munch.play();
                              }
                         }
                         break;
                         case 3: 
                         {
                              Rectangle cellRect = new Rectangle(xbound,ybound,32,32);
                              if(pacRec.intersects(cellRect))
                              {
                                   board[count1][count2]=2;
                                   panic=true;               
                                   //munchPill.play();
                                   //AudioClip munchPill = getAudioClip(getDocumentBase(), "sounds/gong.au");
                                   
                                   /*try{
                                      URL URLaddress2 = new URL("file:" + System.getProperty("user.dir") + "/" + sound2);
                                      clip2 = java.applet.Applet.newAudioClip(URLaddress2);
                                      }catch(MalformedURLException e){
                                      System.err.println("Bad URL:" + e);
                                 }
                                 clip2.play();*/
                              }
                         }
                         break;
                         }
                         xbound = xbound + 32; 
                    }
                    ybound = ybound + 32;
                    xbound = 32;
               }
          scoreLabel.setText("Score: "+score);
          livesLabel.setText("Lives: "+lives);
     }