📄 erixmidlet.java
字号:
//System.out.println("bytHTTPStatus: " + bytHTTPStatus);
// Check the HTTP status retrieved.
//To avoid display of send menu when the application is launched
//for the first time
//indicates HS Upload status data not found but scores exist
if(m_bHighScoresPresent) {
if (bytHTTPStatus == NOT_FOUND || bytHTTPStatus == NOT_UPLOADED) {
m_bSendMenu = true;
m_bClearMenu = true;
}else if (bytHTTPStatus == UPLOADED) {
m_bSendMenu = false;
m_bClearMenu = true;
}
} else {
m_bSendMenu = false;
m_bClearMenu = false;
}
//System.out.println("m_bSendMenu: " + m_bSendMenu);
//System.out.println("m_bClearMenu: " + m_bClearMenu);
/* Create the GameCanvas */
m_objGameCanvas = new GameCanvas(this);
/* Create the Game Engine */
m_objGameEngine = new GameEngine(m_objGameCanvas, this);
/* Set the reference of the Engine in Game Canvas */
m_objGameCanvas.m_objEngine = m_objGameEngine;
/* Set the GameCanvas as the current displayable object */
m_objDisplay.setCurrent(m_objGameCanvas);
// Create the images that are required for all levels in the game.
m_objGameCanvas.createImages();
/* Setting the mode to the splash screen mode */
m_objGameCanvas.update(GameCanvas.SPLASH_SCREEN);
// Instantiate the object to transmit highscores over HTTP. Lazy instantiation.
// m_objHTTPScoreTransmitter = new HTTPScoreTransmitter(this);
/* Get the game language setting from the RMS and store it in a class member */
//m_nIsLanguageSet = retrieveSettings();
m_bIsLanguageSet = retrieveSettings();
/* try to retrieve saved game if any */
//m_nSavedGamePresent = retrieveSavedGame();
m_bIsSavedGamePresent = retrieveSavedGame();
retrieveSerialNumber();
/* For reading language specific strings from file */
createStream();
/* For reading random enemy positions from file*/
createStreamForRandomNumbers();
/* Creates all the images and other items needed for the game */
m_objGameCanvas.initialise();
/* creating MIDlet thread */
Thread objMidletThread = new Thread(this);
/* Starting the MIDlet thread for showing splash screen */
objMidletThread.start();
} // end of if game is not being resumed (i.e., is being launched)
} // end of method startApp
/**
* Signals the ErixMIDlet to enter the paused state.
* This method is called by the Java Application Manager (JAM)
* when the user receives a phone call, or an SMS or MMS message.
* (Exact details depend on the J2ME implementation).
*/
protected void pauseApp(){
m_bResumeApp = true;
}
/**
* Signals the ErixMIDlet to terminate, and enter the destroyed state.
* The destroyApp() method is called by the Java Application Manager (JAM) when the application is to be terminated. This method
* <ul>
* <li> saves the application settings into the RMS,
* <li> saves the current list of high scores into the RMS,
* <li> saves the currently suspended game (if any) into the RMS, and
* <li> frees all objects managed by the ErixMIDlet object.
* </ul>
* If the method encounters the RecordStoreFullException while trying to save the list of high scores or a game, the data (desired to be saved) is lost, and the user is not informed of the same.
*/
protected void destroyApp(boolean bUnconditional) {
/* to kill the midlet thread */
m_bExitMidlet = true;
/* If Midlet enters Destroyed state */
m_bdoCleanUp = false;
m_objDisplay.setCurrent(null);
if (m_objGameEngine != null) {
exitApp();
m_objGameEngine.cleanUp();
if (m_objGameCanvas != null) {
m_objGameCanvas.cleanUp();
}
/* Clean up activities */
m_objDisplay = null;
m_arrHighScores = null;
m_arrDiffLevelHighScores = null;
m_arrLevelForHighScores = null;
m_arrGameData = null;
if (m_arrHighScoreNames!=null) {
int nTemp = m_arrHighScoreNames.length;
for (int i=0;i<nTemp;i++ ) {
m_arrHighScoreNames[i] = null;
}
}
m_arrHighScoreNames = null;
m_objGameCanvas = null;
m_objErixGameDataStore = null;
m_objErixSerialNoStore = null;
m_objErixUploadStatusStore = null;
m_objErixPrefStore = null;
m_objErixHighScoreStore = null;
m_objErixUploadUiStore = null;
m_strHighScoreName = null;
m_objGameEngine = null;
System.gc();
}
notifyDestroyed();
} // end of method destroyApp
/**
* Contains the functionality for the attract mode, and for handling
* the end of a game.
* The ErixMIDlet implements the java.lang.Runnable interface. The
* ErixMIDlet thread is started when the application is started. The
* run() method of the ErixMIDlet thread is usually used to keep track of
* the time elapsed since the last user input, if the application is
* currently displaying the main menu or one of the submenus. After a
* certain amount of time (the 'wait time') has passed since the last
* user input, the application switches to the attract mode. In the
* attract mode the display cycles through a set of screens,
* e.g. game instructions, game credits etc. After the above steps are
* complete, this method changes the display mode to the 'Main
* Menu' mode or the 'Language Preference mode' depending upon
* whether the user's language preference could or could not be
* retrieved from the RMS, respectively.
*/
public void run(){
// nWaitCount = 0; // No of cycles for which Splash screen is to be displayed
m_bEndSplashMode = false; // The splash animation is yet to start.
while (!m_bExitMidlet) {
if(m_bMenuOn){ // To check if the current dispayable is other than canvas
m_bProcessing = m_objDisplay.getCurrent().isShown();
}
if(m_bProcessing){
//Show splash screen if the mode is set to splash screen
if (m_bSplashScreen) {
//m_nSleepTime = 30; // milliseconds
if (m_bEndSplashMode) {
//m_nSleepTime = 500;
if (m_objGameCanvas != null) {
if (!m_bIsLanguageSet) {
// If Game language setting not found
m_objGameCanvas.update(GameCanvas.GAME_LANGUAGE_SELECTION_SCREEN);
} else {
// Setting the mode to start menu mode
m_objGameCanvas.update(GameCanvas.MENU_SCREEN);
}
}
/*
if ( (m_objGameCanvas!=null) && (!m_bIsLanguageSet) ) { // If Game language setting not found
m_objGameCanvas.update(GameCanvas.GAME_LANGUAGE_SELECTION_SCREEN);
}else if (m_objGameCanvas!=null){
// Setting the mode to start menu mode
m_objGameCanvas.update(GameCanvas.MENU_SCREEN);
}
*/
m_bAttractMode = true;
m_bSplashScreen = false;
m_bEndSplashMode = false;
m_nKeyPressWait = 0;
}
}
//activate Attract mode if the mode is set for attract mode
else if (m_bAttractMode) {
if ( (m_objGameCanvas.m_nMode != m_objGameCanvas.GAME_SCREEN)
&& (m_objGameCanvas.m_nMode != m_objGameCanvas.HIGH_SCORE_ENTRY_SCREEN)
&& (m_objGameCanvas.m_nMode != m_objGameCanvas.HELP_SCREEN) ){
m_nSleepTime = 50;
// IMPORTANT NOTE: Whenever the above value is changed,
// update ATTRACT_MODE_SCREEN_WAIT in this class, and
// also ATTRACT_MODE_CONTENT_REFRESH_WAIT in class GameCanvas.
// m_nSleepTime denotes the pause in milliseconds
// after each iteration of the paint cycle, and the
// other two constants denote the number of paint
// cycles to be skipped before refresh the content
// in the respective instance where they are used.
// m_nKeyPressWait++;
if(m_nKeyPressWait <= ATTRACT_MODE_SCREEN_WAIT){
if (m_objGameCanvas.m_nMode != m_objGameCanvas.m_nPreviousMode) {
m_objGameCanvas.update(m_objGameCanvas.m_nPreviousMode);
}
} else if (m_nKeyPressWait == ATTRACT_MODE_SCREEN_WAIT + 1) {
m_objGameCanvas.update(GameCanvas.ATTRACT_SCREEN);
}
}
} else if (m_bAnimScreen) {
//Activate animation on high score screen if the mode is set for animation
m_nSleepTime = 250;
} else {
m_nSleepTime = 0;
}
}
m_objGameCanvas.repaint();
//else wait
try{
Thread.sleep(m_nSleepTime);
} catch (Exception e) {
System.out.println("Exception : " + e);
}
}
/* End of While */
if (m_bdoCleanUp) {
destroyApp(true);
}
}
/**
Handles all the user commands.
Since the ErixMIDlet implements the javax.microedition.lcdui.CommandListener interface, the method commandAction() is called whenever a command event occurs.
The following are the consequences of some of the important commands issued by the user in various modes:
<ul>
<li> While a game is in progress, issuing the 'Back' command results in the game being suspend and its data (nIndex.e level, score,
difficulty level and no of lifes remaining) being saved into the appropriate data members of the Erix MIDlet class, and the application returning to the 'Main Menu' mode.
<li> While a game is in progress, issuing the 'Pause' command results in the game being paused.
<li> While the game is paused, issuing the 'Resume' command results in the game being resumed from the same state in which it was paused.
<li> In the 'Main Menu' mode, issuing the 'Select' command with the game type set to 'Resume' causes the currently suspended game to be resumed at the level from which it was exited.
<li> In the 'New Game' mode, issuing the 'Select' command with the game type set to 'Easy', 'Medium' or 'Hard' causes a new game to be started at the chosen difficulty level.
<li> In the 'Main Menu' mode, issuing the 'Exit' command causes the application to exit after the application settings, the list of high scores and
the data for any suspended game have been saved into the RMS.
If there already was a game saved in the RMS, the data for that is overwritten by the data of the game currently being saved.
A maximum of only one game's data exists in the RMS at any time. Also, if an exception is encountered while writing the settings, the high scores or the game to the RMS,
the application exits nevertheless (and the concerned data is lost).
<li> In the 'High Score View' mode, issuing the 'Send' command causes the current list of high scores to be sent to the high score server.
<li> In the 'High Score View' mode, issuing the 'Reset' command causes the list of high scores to be cleared.
<li> In the 'Language Preference Setting' mode, issuing the 'Select' command causes the application's language to be set as per the choice made by the user in the list of languages.
The application returns to the 'Main Menu' mode, with UI strings of the newly selected language being used.
<li> In the 'Language Preference Setting' mode, issuing the 'Back' command causes the user's current selection in the list of languages to be ignored,
and the language preference of the application being unchanged. If the 'Language Preferences Settings' mode was entered because the application was being used for the first time,
the language preference is set to English by default.
</ul>
*/
public void commandAction(Command objCommand,Displayable objDisplay) {
m_nKeyPressWait = 0; // Reset counter for attract mode
int typeCommand = objCommand.getCommandType();
// If displayable is start menu and command is 'OK'
if ((objDisplay == m_objGameCanvas.m_objListStartMenu)
&& (typeCommand == Command.SCREEN)) { // Patel
// Get the index selected item from the list
m_nSelectedIndex = ((List)objDisplay).getSelectedIndex();
// If saved game is present "Resume Game' Option is present in start menu
if (m_bIsSavedGamePresent) {
if (m_nSelectedIndex == MENU_SCREEN_RESUME) {
// Set flag to indicate that the current game is a resumed one.
m_bIsResumedGame = true;
// Restore the saved game parameter
m_objGameEngine.m_bytLevelNumber = m_arrGameData[0];
m_objGameEngine.m_lTotalScore = m_lGameScore;
m_objGameEngine.m_bytLifeCount = m_arrGameData[2];
m_objGameEngine.m_bytGameType = m_arrGameData[1];
// Set mode to Game play mode
m_objGameCanvas.update(GameCanvas.GAME_SCREEN);
// Start the saved Game
m_objGameEngine.initialiseGameType(ErixMIDlet.RESUME_GAME_STR);
} else {
// Since Erix has a saved game at this point so the index
// of all the items previosly in the list has increased by one,
// so this method handles this condition.
//int nTempStatus = -1;
int nTempStatus = FAILURE;
nTempStatus = handleStartMenu(m_nSelectedIndex, 1);
//if (nTempStatus == -1) {
if (nTempStatus == FAILURE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -