8
Gaming API and Sound
•public void playBackgroundMusic (BackgroundMusic bgm, boolean loop) - Plays the specified BackgroundMusic object from the beginning. This method first stops the current BackgroundMusic if any. Thus, this method may be used to start background music (by specifying a
Using GameScreen
The GameDemoScreen class uses the GameScreen class to provide a UI screen for a hypothetic game. GameDemoScreen is a subclass of GameScreen that implements runnable for running the main game loop thread.
The following code sample shows implementation of using GameScreen.
GameScreen
class GameDemoScreen extends GameScreen implements Runnable{
// ...
public void run() {
//Get the Graphics object for the
//
Graphics g = getGraphics(); while (true) {
//Check user input and update
//positions if necessary
int keyState = getKeyStates(); if ((keyState & LEFT_KEY) != 0) {
}
else if ((keyState & RIGHT_KEY) != 0) { sprite.move(1, 0);
}
//Draw the background g.drawImage(backgroundImage,0,0, Graphics.TOP
+Graphics.LEFT);
//Draw the sprite on top of the background sprite.draw(g);
//Flush the
}
}
// ...
}
43