Motorola C370 Manuale Utente

Pagina di 86

Gaming API and Sound  
 
43 
• 
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 non-null BackgroundMusic 
object), restart the current background music (by specifying the same 
BackgroundMusic object), change the background music, or end the background 
music (by specifying null). The 
loop
 parameter is set to true if the BackgroundMusic 
is to repeat indefinitely. Otherwise, set to false. 
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  
        // off-screen buffer 
        Graphics g = getGraphics(); 
        while (true) { 
            // Check user input and update  
            // positions if necessary 
            int keyState = getKeyStates(); 
            if ((keyState & LEFT_KEY) != 0) { 
                sprite.move(-1, 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 off-screen buffer  
            flushGraphics(); 
        } 
    } 
    // ...