From b4bc4bd9ab84d15554d0c187a72770b73a01edac Mon Sep 17 00:00:00 2001 From: forteri76 Date: Wed, 14 Aug 2013 12:52:32 +0000 Subject: [PATCH] Fixed issue whereby a game that was being created from a Bundle was overriding the game mode selected (Puzzle or Player vs CPU). --- .../efortin/frozenbubble/SplashScreen.java | 7 +- src/org/jfedor/frozenbubble/FrozenBubble.java | 140 ++++++++++-------- 2 files changed, 83 insertions(+), 64 deletions(-) diff --git a/src/com/efortin/frozenbubble/SplashScreen.java b/src/com/efortin/frozenbubble/SplashScreen.java index ca65946..7147992 100644 --- a/src/com/efortin/frozenbubble/SplashScreen.java +++ b/src/com/efortin/frozenbubble/SplashScreen.java @@ -109,7 +109,7 @@ private void addHomeButtons() { start2pGameButton.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ - // Process the button tap and start a 2 player game. + // Process the button tap and start/resume a 2 player game. startFrozenBubble(2); } }); @@ -215,7 +215,7 @@ public void onCreate(Bundle savedInstanceState) { LayoutParams.FILL_PARENT)); myImageView = new ImageView(this); - if (FrozenBubble.isRunning) + if (FrozenBubble.numPlayers != 0) startFrozenBubble(FrozenBubble.numPlayers); else if (getIntent().hasExtra("startHomeScreen")) { setBackgroundImage(R.drawable.home_screen); @@ -354,8 +354,7 @@ private void startFrozenBubble(int numPlayers) { // // Intent intent = new Intent(this, FrozenBubble.class); - if (numPlayers > 1) - intent.putExtra("numPlayers", (int)numPlayers); + intent.putExtra("numPlayers", (int)numPlayers); startActivity(intent); // // Terminate the splash screen activity. diff --git a/src/org/jfedor/frozenbubble/FrozenBubble.java b/src/org/jfedor/frozenbubble/FrozenBubble.java index f5ebc8d..ceadd9d 100644 --- a/src/org/jfedor/frozenbubble/FrozenBubble.java +++ b/src/org/jfedor/frozenbubble/FrozenBubble.java @@ -151,8 +151,7 @@ public class FrozenBubble extends Activity public final static int POINT_TO_SHOOT = 1; public final static int ROTATE_TO_SHOOT = 2; - public static boolean isRunning = false; - public static int numPlayers = 0; + public static int numPlayers = 0; private static boolean adsOn = true; private static int collision = BubbleSprite.MIN_PIX; @@ -219,7 +218,6 @@ public void onCreate(Bundle savedInstanceState) { // Log.i(TAG, "FrozenBubble.onCreate(null)"); //} super.onCreate(savedInstanceState); - isRunning = true; setVolumeControlStream(AudioManager.STREAM_MUSIC); requestWindowFeature(Window.FEATURE_NO_TITLE); restoreGamePrefs(); @@ -237,46 +235,14 @@ public void onOrientationChanged(int arg0) { // Allow editor functionalities. Intent intent = getIntent(); - if ((null == intent) || - (null == intent.getExtras()) || - !intent.getExtras().containsKey("levels")) { - // Default levels. - activityCustomStarted = false; - // Check if this is a single player or multiplayer game. - numPlayers = 1; - if (intent.hasExtra("numPlayers")) - numPlayers = intent.getIntExtra("numPlayers", 1); - if (numPlayers > 1) { - mMultiplayerGameView = new MultiplayerGameView(this, numPlayers); - setContentView(mMultiplayerGameView); - mMultiplayerGameView.setGameListener(this); - mMultiplayerGameThread = mMultiplayerGameView.getThread(); - if (savedInstanceState != null) { - int savedPlayers = savedInstanceState.getInt("numPlayers"); - if (savedPlayers == 2) - mMultiplayerGameThread.restoreState(savedInstanceState); - } - mMultiplayerGameThread.startOpponent(); - mMultiplayerGameView.requestFocus(); - } - else { - setContentView(R.layout.activity_frozen_bubble); - mGameView = (GameView)findViewById(R.id.game); - mGameView.setGameListener(this); - mGameThread = mGameView.getThread(); - if (savedInstanceState != null) { - int savedPlayers = savedInstanceState.getInt("numPlayers"); - if (savedPlayers == 1) - mGameThread.restoreState(savedInstanceState); - } - - mGameView.requestFocus(); - } - setFullscreen(); - playMusic(false); - } - else { - startCustomGame(intent); + try { + if ((null == intent) || (null == intent.getExtras()) || + !intent.getExtras().containsKey("levels")) + startDefaultGame(intent, savedInstanceState); + else + startCustomGame(intent); + } catch (NullPointerException npe) { + startDefaultGame(intent, savedInstanceState); } } @@ -384,7 +350,7 @@ public void onOptionsMenuClosed(Menu menu) { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { - isRunning = false; + numPlayers = 0; // // Preserve game information and perform activity cleanup. // @@ -426,7 +392,7 @@ protected void onPause() { protected void onDestroy() { //Log.i(TAG, "FrozenBubble.onDestroy()"); super.onDestroy(); - isRunning = false; + numPlayers = 0; cleanUp(); } @@ -454,18 +420,19 @@ protected void onSaveInstanceState(Bundle outState) { */ @Override protected void onNewIntent(Intent intent) { - if (null != intent && EDITORACTION.equals(intent.getAction())) { - if (mGameView != null) - mGameView.cleanUp(); - mGameView = null; - mGameThread = null; - - if (mMultiplayerGameView != null) - mMultiplayerGameView.cleanUp(); - mMultiplayerGameView = null; - mMultiplayerGameThread = null; + if (null != intent) { + if (EDITORACTION.equals(intent.getAction())) { + cleanUpGameView(); + startCustomGame(intent); + } + else if ((numPlayers != 0) && intent.hasExtra("numPlayers")) { + int newNumPlayers = intent.getIntExtra("numPlayers", 1); - startCustomGame(intent); + if (newNumPlayers != numPlayers) { + cleanUpGameView(); + startDefaultGame(intent, null); + } + } } } @@ -626,6 +593,55 @@ private void startCustomGame(Intent intent) { playMusic(false); } + /** + * Method to start a game using default levels, if puzzle game mode + * was selected. + * + * @param intent + * - The intent used to start this activity. + * @param savedInstanceState + * - the bundle of saved state information. + */ + private void startDefaultGame(Intent intent, Bundle savedInstanceState) { + // Default levels. + activityCustomStarted = false; + // Check if this is a single player or multiplayer game. + numPlayers = 1; + if (intent != null) { + if (intent.hasExtra("numPlayers")) + numPlayers = intent.getIntExtra("numPlayers", 1); + } + + if (numPlayers > 1) { + mMultiplayerGameView = new MultiplayerGameView(this, numPlayers); + setContentView(mMultiplayerGameView); + mMultiplayerGameView.setGameListener(this); + mMultiplayerGameThread = mMultiplayerGameView.getThread(); + if (savedInstanceState != null) { + int savedPlayers = savedInstanceState.getInt("numPlayers"); + if (savedPlayers == 2) + mMultiplayerGameThread.restoreState(savedInstanceState); + } + mMultiplayerGameThread.startOpponent(); + mMultiplayerGameView.requestFocus(); + } + else { + setContentView(R.layout.activity_frozen_bubble); + mGameView = (GameView)findViewById(R.id.game); + mGameView.setGameListener(this); + mGameThread = mGameView.getThread(); + if (savedInstanceState != null) { + int savedPlayers = savedInstanceState.getInt("numPlayers"); + if (savedPlayers == 1) + mGameThread.restoreState(savedInstanceState); + } + + mGameView.requestFocus(); + } + setFullscreen(); + playMusic(false); + } + private void setFullscreen() { final int flagFs = WindowManager.LayoutParams.FLAG_FULLSCREEN; final int flagNoFs = WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; @@ -861,6 +877,14 @@ public void cleanUp() { myOrientationEventListener = null; } + cleanUpGameView(); + + if (myModPlayer != null) + myModPlayer.destroyMusicPlayer(); + myModPlayer = null; + } + + private void cleanUpGameView() { if (mGameView != null) mGameView.cleanUp(); mGameView = null; @@ -870,10 +894,6 @@ public void cleanUp() { mMultiplayerGameView.cleanUp(); mMultiplayerGameView = null; mMultiplayerGameThread = null; - - if (myModPlayer != null) - myModPlayer.destroyMusicPlayer(); - myModPlayer = null; } /**