每當我按下返回按鈕時將我帶回到MenuScreen,我無法停止我的GameScreen背景音樂。音樂繼續播放,MenuScreen音樂開始播放,聽起來很糟糕。這是我的MusicManager類。當按下返回按鈕時,背景音樂不會停止(返回菜單屏幕)Android
public class MusicManager implements MediaPlayer.OnCompletionListener{
AudioManager amg;
private MediaPlayer mMediaPlayer;
private MediaPlayer mMediaPlayer1;
private String mAssetFile;
private boolean mIsPrepared = false;
private boolean mIsPrepared1 = false;
private ScreenManager mScreenManager;
public MusicManager(Game game, ScreenManager mScreenManager) {
super();
mScreenManager = new ScreenManager();
mMediaPlayer = new MediaPlayer();
mMediaPlayer1 = new MediaPlayer();
try{ AssetManager assetManager = game.getActivity().getAssets();
AssetFileDescriptor assetDescriptor = assetManager.openFd("Sounds/GameScreen1.wav");
AssetFileDescriptor assetDescriptor1 = assetManager.openFd(("Sounds/Game-Menu.ogg"));
//assetDescriptor = assets.openFd("clicksound");
mMediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
assetDescriptor.getStartOffset(),
assetDescriptor.getLength());
mMediaPlayer1.setDataSource(assetDescriptor1.getFileDescriptor(),
assetDescriptor1.getStartOffset(),
assetDescriptor1.getLength());
// Prep the audio for playback
mMediaPlayer.prepare();
mMediaPlayer1.prepare();
mIsPrepared = true;
mIsPrepared1 = true;
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer1.setOnCompletionListener(this);
} catch (IOException e) {
String errorTag = "Error:";
String errorMessage = "Music clip cannot be loaded.";
Log.w(errorTag, errorMessage);
}
}
public void play() {
if (mMediaPlayer.isPlaying())
return;
try {
synchronized (this) {
if (!mIsPrepared)
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (Exception e) {
String errorTag = "Error:";
String errorMessage = "Music clip1 cannot be played.";
Log.w(errorTag, errorMessage);
}
}
public void play1() {
if (mMediaPlayer1.isPlaying())
return;
try {
synchronized (this) {
if (!mIsPrepared1)
mMediaPlayer1.prepare();
mMediaPlayer1.start();
}
} catch (Exception e) {
String errorTag = "Error:";
String errorMessage = "Music clip cannot be played.";
Log.w(errorTag, errorMessage);
}
}
public void stop() {
mMediaPlayer.stop();
synchronized (this) {
mIsPrepared = false;
}
}
public void stop1() {
mMediaPlayer1.stop();
synchronized (this) {
mIsPrepared1 = false;
}
}
public void pause() {
if (mMediaPlayer.isPlaying())
mMediaPlayer.pause();
}
public void pause1() {
if (mMediaPlayer1.isPlaying())
mMediaPlayer1.pause();
}
/**
* Determine if the music clip will loop
*
* @param looping
* Boolean true to loop, false for play once.
*/
public void setLooping(boolean looping) {
mMediaPlayer.setLooping(looping);
}
public void setLooping1(boolean looping) {
mMediaPlayer1.setLooping(looping);
}
/**
* Set the playback volume
*
* @param volume
* Playback volume (0-1)
*/
public void setVolume(float volume) {
mMediaPlayer.setVolume(volume, volume);
}
public void setVolume1(float volume) {
mMediaPlayer1.setVolume(volume, volume);
}
/**
* Set the playback volume
*
* @param leftVolume
* Left channel playback volume (0-1)
* @param rightVolume
* Right channel playback volume (0-1)
*/
public void setVolume(float leftVolume, float rightVolume) {
mMediaPlayer.setVolume(leftVolume, rightVolume);
}
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
public boolean isPlaying1() {
return mMediaPlayer1.isPlaying();
}
public boolean isLooping() {
return mMediaPlayer.isLooping();
}
public boolean isLooping1() {
return mMediaPlayer1.isLooping();
}
public void dispose() {
if(mScreenManager.getCurrentScreen().getName().equals("Menu Screen"))
if (mMediaPlayer.isPlaying())
mMediaPlayer.stop();
mMediaPlayer.release();
}
public void dispose1() {
if (mMediaPlayer1.isPlaying())
mMediaPlayer1.stop();
mMediaPlayer1.release();
}
@Override
public void onCompletion(MediaPlayer player) {
synchronized (this) {
mIsPrepared = false;
}
}
這裏是哪裏的聲音被初始化,並在GameScreen類開始,也是我現在用停止音樂的onBackPressed函數的代碼。
public CardDemoGameScreen(Game game) {
super("CardDemoGameScreen", game);
mSoundManager = new SoundManager(game);
mScreenManager = new ScreenManager();
mMusicManager = new MusicManager(game, mScreenManager);
...
public void update(ElapsedTime elapsedTime) {
//NOTE: elapsed time has time between frames AND time since program
started
mMusicManager.play();
mMusicManager.isPlaying();
mMusicManager.setLooping(true);
mMusicManager.isLooping();
mMusicManager.setVolume(0.1f);
...
@Override
public boolean onBackPressed() {
mMusicManager.dispose();
return true;
}
我已經試過絕對一切!我嘗試過使用onKeyDown這不起作用,我試着在MenuScreen類的末尾開始聲音,並在那裏停止播放,這也不起作用。 任何幫助,將不勝感激
編輯 GAMESCREEN CLASS
public class CardDemoGameScreen extends GameScreen {
private final float LEVEL_WIDTH = 1000.0f;
private final float LEVEL_HEIGHT = 1500.0f;
//value less than 1 means everything appears smaller, and vice versa
private float worldZoom;
GamePhase CurrentState = PHASE_DRAW;
private ScreenViewport mScreenViewport;
private LayerViewport mLayerViewport;
private Player mPlayer;
private Scribe mScribe;
private SubjectHelper mSubjectHelper;
//gameobject because it only needs a position and a bitmap
private GameObject mBackground;
private AssetStore assetManager = mGame.getAssetManager();
private GameEventHandler mGameEventHandler;
private Deck mDeck;
boolean newTurn = false;
boolean newPhase = false;
private SoundManager mSoundManager;
private MusicManager mMusicManager;
private ScreenManager mScreenManager;
public CardDemoGameScreen(Game game) {
super("CardDemoGameScreen", game);
mSoundManager = new SoundManager(game);
mScreenManager = new ScreenManager();
mMusicManager = new MusicManager(game, mScreenManager);
//mLayerViewport = new LayerViewport(240, 160, 480, 320);
//mScreenViewport = new ScreenViewport();
//GraphicsHelper.create3To2AspectRatioScreenViewport(game, mScreenViewport);
//the (0,0) puts it in the TOP left corner, then makes it the screen wide and the screen tall
mScreenViewport = new ScreenViewport(0, 0, game.getScreenWidth(), game.getScreenHeight());
mSubjectHelper = new SubjectHelper();
Card.setSubjectHelper(mSubjectHelper);
float shorterEdge = 480.0f; //TODO: make this a proper const
//so the layer viewport is the same aspect ratio as the screenviewport but a different size
if (mScreenViewport.width > mScreenViewport.height)
mLayerViewport = new LayerViewport(shorterEdge, shorterEdge * mScreenViewport.height/mScreenViewport.width, //this should automatically take care of any weird scaling but it somehow doesnt?
shorterEdge, shorterEdge * mScreenViewport.height/mScreenViewport.width);
else
mLayerViewport = new LayerViewport(shorterEdge * mScreenViewport.height/mScreenViewport.width, shorterEdge,
shorterEdge * mScreenViewport.height/mScreenViewport.width, shorterEdge);
worldZoom = 1.0f;
assetManager.loadAndAddBitmap("TrapCardTemplate", "img/trapcardtemplate.png");
assetManager.loadAndAddBitmap("UnimonTemplate", "img/monstertemplate.png");
assetManager.loadAndAddBitmap("ExpandedUnimonTemplate", "img/monstertemplateexpanded.png");
assetManager.loadAndAddBitmap("ExpandedTrapcardTemplate", "img/trapcardtemplateexpanded.png");
assetManager.loadAndAddBitmap("ExpandedSupportcardTemplate", "img/supportcardtemplateexpanded.png");
assetManager.loadAndAddBitmap("CharSheet", "img/charactersheettrans.png");
assetManager.loadAndAddBitmap("SupportCardTemplate", "img/supportcardtemplate.png");
assetManager.loadAndAddBitmap("Subject EEECS", "img/EEECS30.png");
assetManager.loadAndAddBitmap("Subject Civil", "img/civil30.png");
assetManager.loadAndAddBitmap("Subject Mech", "img/Mechanical30.png");
assetManager.loadAndAddBitmap("Subject Phil", "img/Philisophy30.png");
assetManager.loadAndAddBitmap("Subject Art", "img/art30.png");
assetManager.loadAndAddBitmap("Subject Engl", "img/english30.png");
assetManager.loadAndAddBitmap("Subject Phy", "img/physics30.png");
assetManager.loadAndAddBitmap("Subject Medic", "img/medicine30.png");
assetManager.loadAndAddBitmap("Subject Math", "img/math30.png");
assetManager.loadAndAddBitmap("Selected", "img/selected.png");
assetManager.loadAndAddBitmap("Background", "img/belfastmap.png");
assetManager.loadAndAddBitmap("DeckIcon", "img/DeckIcon.png");
TrapCard.setTemplate(assetManager.getBitmap("TrapCardTemplate"));
TrapCard.setExpandedTemplate(assetManager.getBitmap("ExpandedTrapcardTemplate"));
Unimon.setTemplate(assetManager.getBitmap("UnimonTemplate"));
Unimon.setExpandedTemplate(assetManager.getBitmap("ExpandedUnimonTemplate"));
SupportCard.setTemplate(assetManager.getBitmap("SupportCardTemplate"));
SupportCard.setExpandedTemplate(assetManager.getBitmap("ExpandedSupportcardTemplate"));
mGameEventHandler = new GameEventHandler();
mGameEventHandler.ResetFlags();
mPlayer = new Player(this, mGameEventHandler, game);
mPlayer.setSelectedBitmap(assetManager.getBitmap("Selected"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_EEECS, assetManager.getBitmap("Subject EEECS"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_CIVIL, assetManager.getBitmap("Subject Civil"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_MECH, assetManager.getBitmap("Subject Mech"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_ENGL, assetManager.getBitmap("Subject Engl"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_ART, assetManager.getBitmap("Subject Art"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_PHIL, assetManager.getBitmap("Subject Phil"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_MEDIC, assetManager.getBitmap("Subject Medic"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_PHY, assetManager.getBitmap("Subject Phy"));
mSubjectHelper.setSubjectGraphic(Subject.SUBJECT_MATH, assetManager.getBitmap("Subject Math"));
mScribe = new Scribe(assetManager.getBitmap("CharSheet"), 8, 16, 9, 15);
mBackground = new GameObject(LEVEL_WIDTH/2.0f, LEVEL_HEIGHT/2.0f, assetManager.getBitmap("Background"), this);
//All the set up for creating slots
assetManager.loadAndAddBitmap("TrapCardSlot", "img/TrapCardSlot.png");
assetManager.loadAndAddBitmap("BenchImage", "img/benchcardslot.png");
assetManager.loadAndAddBitmap("SupportCardSlot", "img/supportcardslot.png");
assetManager.loadAndAddBitmap("DeckImage","img/DeckIcon.png");
Bitmap TrapCardSlotImage = assetManager.getBitmap("TrapCardSlot");
Bitmap BenchImage = assetManager.getBitmap("BenchImage");
assetManager.loadAndAddBitmap("SupportCardSlot", "img/supportcardslot.png");
Bitmap SupportCardSlotImage = assetManager.getBitmap("SupportCardSlot");
Bitmap DeckImage = assetManager.getBitmap("DeckImage");
mPlayer.createSlots(TrapCardSlotImage, BenchImage, SupportCardSlotImage, DeckImage, LEVEL_WIDTH, LEVEL_HEIGHT);
//TODO: James - shouldn't this belong to player?
mDeck = new Deck(this, 10);
//TODO: this could be an addUnimon function to make things cleaner
mPlayer.getCards().add(new SupportCard("Supporty McSupportface", "Effect Description", Subject.SUBJECT_MATH, 3, 500, 400, assetManager.getBitmap("FirstImg"), this));
Move move1 = new Move("Attacky attack", "Attack confuses opponent programmer", 90, Subject.SUBJECT_MECH);
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
mPlayer.getCards().add(new Unimon("Unimon Name", 100, move1, move1, Subject.SUBJECT_EEECS, 1, 800, 400, assetManager.getBitmap("UnimonImage"), this));
}
public void update(ElapsedTime elapsedTime) {
//NOTE: elapsed time has time between frames AND time since program started
mMusicManager.play();
mMusicManager.isPlaying();
mMusicManager.setLooping(true);
mMusicManager.isLooping();
mMusicManager.setVolume(0.1f);
mPlayer.update(elapsedTime);
mPlayer.updateLayer(mLayerViewport, 0, LEVEL_WIDTH, 0, LEVEL_HEIGHT);
mPlayer.HandleTouch(mScreenViewport, mLayerViewport, worldZoom);
mPlayer.updateHandLayout(LEVEL_WIDTH, LEVEL_HEIGHT);
switch(CurrentState){
case PHASE_DRAW:
//pull a card from the deck and place it in the players hand
mPlayer.addCardToHand(mDeck.getTopCard());
CurrentState = PHASE_REINFORCEMENT;
newPhase = true;
break;
case PHASE_REINFORCEMENT:
if (mPlayer.getActiveSlot().getCardinSlot() == null) {
if (mPlayer.anyLevel1Cards()) {
mPlayer.setAllLevel1CardsSelectable();
} else {
//GAME OVER STUFF HERE
}
} else if(mGameEventHandler.GetFlag(GameEvent.EVENT_MONSTER_PLACED_ACTIVE)) {
mSoundManager.playSound(4, 0.2f);
if (mPlayer.isSelectedCardInHand(mPlayer.getActiveSlot().getCardinSlot())) {
mPlayer.removeCardFromHand(mPlayer.getActiveSlot().getCardinSlot());
mPlayer.setCardInActiveSlot(mPlayer.getActiveSlot().getCardinSlot());
CurrentState = PHASE_BATTLE;
mSoundManager.playSound(5, 0.4f);
newPhase = true;
}
} else {
CurrentState = PHASE_BATTLE;
mSoundManager.playSound(5, 0.4f);
newPhase = true;
}
break;
case PHASE_BATTLE:
//only active unimon is selectable
//state transition if a move is selected
mPlayer.getActiveSlot().getCardinSlot().setSelectable();
if (mGameEventHandler.GetFlag(GameEvent.EVENT_ATTACK)) {
CurrentState = PHASE_MAIN;
newPhase = true;
}
break;
case PHASE_MAIN:
//all player cards selectable, expandable
//and some forms of placement will advance the game states
mPlayer.setAllCardsSelectable();
if(mGameEventHandler.GetFlag(GameEvent.EVENT_MONSTER_PLACED)
|| mGameEventHandler.GetFlag(GameEvent.EVENT_SUPPORT_PLACED)
|| mGameEventHandler.GetFlag(GameEvent.EVENT_EVOLUTION)
|| mGameEventHandler.GetFlag(GameEvent.EVENT_TRAP_PLACED)) {
mSoundManager.playSound(4, 0.2f);
//this is where we would change gamephase
CurrentState = PHASE_DRAW;
newTurn = true;
newPhase = true;
}
break;
}
if (newPhase) {
mPlayer.setAllCardsNotSelectable();
mPlayer.setSelectedCard(null);
mPlayer.setExpandedCard(null);
newPhase = false;
}
if (newTurn) {
for (Card CardToUpdate : mPlayer.getCards()) {
if (CardToUpdate.getCardType() == CardType.SUPPORT) {
//decrement the moves
((SupportCard)CardToUpdate).DecrementTurnsActive();
}
}
newTurn = false;
}
mGameEventHandler.ResetFlags();
}
@Override
public boolean onBackPressed() {
mMusicManager.dispose();
return true;
}
public void draw(ElapsedTime elapsedTime, IGraphics2D graphics2D) {
//graphics2D.clear(Color.MAGENTA);
mBackground.drawZoomed(elapsedTime, worldZoom, graphics2D, mLayerViewport, mScreenViewport, null);
for (Slot SlotToDraw : mPlayer.getSlots()) {
SlotToDraw.drawZoomed(elapsedTime, worldZoom, mScribe, graphics2D, mLayerViewport, mScreenViewport);
}
for (Card cardToDraw : mPlayer.getCards()) {
cardToDraw.drawZoomed(elapsedTime, worldZoom, mScribe, graphics2D, mLayerViewport, mScreenViewport);
}
if (mPlayer.getSelectedCard() != null) {
mPlayer.drawSelectedCard(elapsedTime, worldZoom, graphics2D, mLayerViewport, mScreenViewport);
}
if (mPlayer.getExpandedCard() != null) {
mPlayer.drawExpandedCard(elapsedTime, worldZoom, mScribe, graphics2D, mLayerViewport, mScreenViewport);
}
//mScribe.drawParagraphWorldNarrow("the quick brown fox jumps over lazy dog", 100, 100, 60, worldZoom, mLayerViewport, mScreenViewport, graphics2D);
//mScribe.drawParagraphScreenNarrow("the quick brown fox jumps over lazy dog", 100, 100, 60, mScreenViewport.height, graphics2D);
switch(CurrentState){
case PHASE_DRAW:
mScribe.drawParagraphScreenNarrow("DRAW PHASE", mScreenViewport.right-100, 20, 100, graphics2D);
break;
case PHASE_REINFORCEMENT:
mScribe.drawParagraphScreenNarrow("REINFORCEMENT PHASE", mScreenViewport.right-100, 20, 100, graphics2D);
break;
case PHASE_BATTLE:
mScribe.drawParagraphScreenNarrow("BATTLE PHASE", mScreenViewport.right-100, 20, 100, graphics2D);
break;
case PHASE_MAIN:
mScribe.drawParagraphScreenNarrow("MAIN PHASE", mScreenViewport.right-100, 20, 100, graphics2D);
break;
}
}
public float getWorldZoom() { return worldZoom; }
/**
* Change WorldZoom
*
* @param newWorldZoom
* Float value. No zoom = 1.0f. To zoom in increase number
*/
public void setWorldZoom(float newWorldZoom) { worldZoom = newWorldZoom; }
public float getLEVEL_WIDTH() { return LEVEL_WIDTH; }
public float getLEVEL_HEIGHT() { return LEVEL_HEIGHT; }
public Player getPlayer() {return mPlayer; }
public AssetStore getAssetManager(){return assetManager; }
}
編輯 MENUSCREEN
private SoundManager mSoundManager;
private MusicManager mMusicManager;
public Boolean soundMuted = false;
private ScreenManager mScreenManager;
/**
* Create a simple menu screen
*
* @param game
* Game to which this screen belongs
*/
public MenuScreen(Game game) {
super("MenuScreen", game);
mScreenManager = new ScreenManager();
mMusicManager = new MusicManager(game, mScreenManager);
mSoundManager = new SoundManager(game);
...
@Override
public void update(ElapsedTime elapsedTime) {
// Process any touch events occurring since the update
Input input = mGame.getInput();
if(!soundMuted && !mMusicManager.isPlaying1()) {
mMusicManager.setLooping1(true);
mMusicManager.setVolume1(0.4f);
mMusicManager.play1();
mMusicManager.isPlaying1();
mMusicManager.isLooping1();
}
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchEvent = touchEvents.get(0);
if (touchEvent.type == TouchEvent.TOUCH_UP) {
if ((soundButtonBound.contains((int) touchEvent.x, (int) touchEvent.y))) {
soundMuted = !soundMuted; //invert value
}
if (mPlayButtonBound.contains((int) touchEvent.x, (int) touchEvent.y)) {
mSoundManager.playSound(2, 1);
mGame.getScreenManager().removeScreen(this.getName());
CardDemoGameScreen cardDemoGameScreen = new CardDemoGameScreen(mGame);
mGame.getScreenManager().addScreen(cardDemoGameScreen);
mMusicManager.pause1();
}
if (mInventoryButtonBound.contains((int) touchEvent.x, (int) touchEvent.y)) {
mSoundManager.playSound(2, 1);
mGame.getScreenManager().removeScreen(this.getName());
InventoryScreen inventoryScreen = new InventoryScreen(mGame);
mGame.getScreenManager().addScreen(inventoryScreen);
}
}
}
if (soundMuted) {
if (mMusicManager.isPlaying1())
mMusicManager.pause1();
}
if(mMusicManager.isPlaying()) {
mMusicManager.stop();
}
}
...
@Override
public boolean onBackPressed() {
mMusicManager.dispose();
return false;
}
}
編輯所在班級GAMESCREEN被丟棄
public class DemoGame extends Game {
/**
* Create a new demo game
*/
public DemoGame() {
super();
}
/*
* (non-Javadoc)
*
* @see uk.ac.qub.eeecs.gage.Game#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Go with a default 20 UPS/FPS
setTargetFramesPerSecond(20);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Call the Game's onCreateView to get the view to be returned.
View view = super.onCreateView(inflater, container, savedInstanceState);
// Create and add a stub game screen to the screen manager. We don't
// want to do this within the onCreate method as the menu screen
// will layout the buttons based on the size of the view.
MenuScreen stubMenuScreen = new MenuScreen(this);
mScreenManager.addScreen(stubMenuScreen);
return view;
}
@Override
public boolean onBackPressed() {
// If we are already at the menu screen then exit
if(mScreenManager.getCurrentScreen().getName().equals("MenuScreen"))
return false;
// Go back to the menu screen
getScreenManager().removeScreen(mScreenManager.getCurrentScreen().getName());
//MusicManager mMusicManager = new MusicManager(this, mScreenManager);
//mMusicManager.dispose();
MenuScreen menuScreen = new MenuScreen(this);
getScreenManager().addScreen(menuScreen);
return true;
}
}
GAMESCREEN CLASS
public abstract class GameScreen {
// /////////////////////////////////////////////////////////////////////////
// Properties
// /////////////////////////////////////////////////////////////////////////
/**
* Name that is given to this game screen
*/
protected final String mName;
/**
* Return the name of this game screen
*
* @return Name of this game screen
*/
public String getName() {
return mName;
}
/**
* Game to which game screen belongs
*/
protected final Game mGame;
/**
* Return the game to which this game screen is attached
*
* @return Game to which screen is attached
*/
public Game getGame() {
return mGame;
}
// /////////////////////////////////////////////////////////////////////////
// Constructors
// /////////////////////////////////////////////////////////////////////////
/**
* Create a new game screen associated with the specified game instance
*
* @param game
* Game instance to which the game screen belongs
*/
public GameScreen(String name, Game game) {
mName = name;
mGame = game;
}
// /////////////////////////////////////////////////////////////////////////
// Update and Draw
// /////////////////////////////////////////////////////////////////////////
/**
* Update the game screen. Invoked automatically from the game.
*
* NOTE: If the update is multi-threaded control should not be returned from
* the update call until all update processes have completed.
*
* @param elapsedTime
* Elapsed time information for the frame
*/
public abstract void update(ElapsedTime elapsedTime);
public abstract boolean onBackPressed();
/**
* Draw the game screen. Invoked automatically from the game.
*
* @param elapsedTime
* Elapsed time information for the frame
* @param graphics2D
* Graphics instance used to draw the screen
*/
public abstract void draw(ElapsedTime elapsedTime, IGraphics2D graphics2D);
// /////////////////////////////////////////////////////////////////////////
// Android Life Cycle
// /////////////////////////////////////////////////////////////////////////
/**
* Invoked automatically by the game whenever the app is paused.
*/
public void pause() {
}
/**
* Invoked automatically by the game whenever the app is resumed.
*/
public void resume() {
}
/**
* Invoked automatically by the game whenever the app is disposed.
*/
public void dispose() {
}
}
MAINACTIVTY
public class MainActivity extends Activity {
/**
* Game fragment instance
*/
private Game mGame;
if (mGame == null) {
mGame = new DemoGame();
fm.beginTransaction().add(R.id.activity_fragment_id, mGame)
.commit();
}
}
/*
* (non-Javadoc)
* @see android.app.Activity#onBackPressed()
*/
@Override
public void onBackPressed() {
// If the fragment does not consume the back event then
// trigger the default behaviour
if(!mGame.onBackPressed())
super.onBackPressed();
}
}
看起來你是從'屏幕',而不是'菜單屏幕'調用mMusicManager.dispose()。易於與調試器檢查,.. – GreyBeardedGeek
我曾嘗試從MenuScreen類調用它也沒有運氣.. –