|
Audio in the main class (the applet) are easy; see oo.
If you are putting them in one of your classes that get called
by the applet, its just a little trickier. The class that is used
needs to know about the applet.
For example if we had an applet class called Main and a Spaceship
class that we wanted to play a sound.
In Main when we declare the Spaceship we might:
Spaceship theSpaceship = new SpaceShip(x,y,this);
We have added the word this -> which is referring to this applet.
Now the Spaceship (which had parameters x,y) will take in a parameter
that is the applet.
In Spaceship our constructor looks like:
//Globals to keep track of the image and the applet
AudioClip clip;
Applet myApplet;
public Spaceship(int x, int y, Applet theApplet)
{
myApplet=theApplet;
//Now I will load the img
clip = myApplet.getAudioClip(myApplet.getCodeBase(), "sound.wav");
}
To play the sound we just say clip.play();
public void playSound()
{
clip.play();
}
|