⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 erixmidlet.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            // ... and if the user has chosen the "Ok" command ...
            if (objCommand == m_objGameCanvas.objOkCmd) {
                m_objGameCanvas.update(GameCanvas.USER_AUTHENTICATION_SCREEN);
            }
        }

        // If the highscores upload result form is being shown ...
        if (objDisplay == m_objGameCanvas.m_objHighscoresUploadResultForm) {
            // ... and if the user has chosen the "Ok" or the "No" command ...
            if ((objCommand == m_objGameCanvas.objOkCmd)
                || (objCommand == m_objGameCanvas.objNoCmd)
                || (objCommand == m_objGameCanvas.objNoDummyCmd)) {
                // 1. The "Ok" command is present on the form if the upload
                // was successful, or if the upload failed for a reason
                // other than an authentication error. In both cases,
                // we need to take the user back to the list of highscores.
                // 2. The "No" command is present only if there was a
                // user authentication error. If the user has chosen "No",
                // it means they don't want to try the upload operation again,
                // at least immediately. In this case, we have to take the user
                // back to the high scores list.
                m_objGameCanvas.update(GameCanvas.HIGH_SCORE_LIST_SCREEN);

            } else {
                // The command must be the "Yes" command, which is present
                // on the form when there has been an authentication error.
                // If the user has chosen "Yes", it means they want
                // to attempt the upload again.
                // So, we need to take the user to the authentication screen.
                m_objGameCanvas.update(GameCanvas.USER_AUTHENTICATION_SCREEN);
            }
        }

        if (objDisplay == m_objGameCanvas.m_objListGameDifficulty
                && typeCommand == Command.SCREEN) { // Patel
            /* Get the index selected item from the list*/
            int nSelectedIndex = ((List)objDisplay).getSelectedIndex();
            if (nSelectedIndex<3) { //not sure if this is necessary but it never hurts to leave it as it is.
                switch (nSelectedIndex) {
                    case ErixMIDlet.NEW_GAME_EASY:
                        m_nGameDifficulty = NEW_GAME_EASY;
                        break;
                    case ErixMIDlet.NEW_GAME_MEDIUM:
                        m_nGameDifficulty = NEW_GAME_MEDIUM;
                        break;
                    case ErixMIDlet.NEW_GAME_HARD:
                        m_nGameDifficulty = NEW_GAME_HARD;
                        break;
/*
                    case ErixMIDlet.RESUME_GAME_STR:
                        // Set the mode of the Erix to Game mode and the saved game is started
                          m_objGameEngine.m_bytLevelNumber = m_arrGameData[0];
                          m_objGameEngine.m_lTotalScore = m_lGameScore;
                          m_objGameEngine.m_bytLifeCount = m_arrGameData[2];
                        break;
*/
                    default:
                        break;
                }
                // Set mode to Game play mode
                m_objGameEngine.m_bytLevelNumber = 1;
	            // Set a flag to indicate that the current game is
	            // not a resumed one.
	            m_bIsResumedGame = false;
                m_objGameEngine.initialiseGameType(m_nGameDifficulty);
                m_objGameCanvas.update(GameCanvas.GAME_SCREEN);
            }
        }else if(objDisplay == m_objGameCanvas.m_objListGameDifficulty
                    && objCommand == m_objGameCanvas.objBackCmd
                    || objCommand == m_objGameCanvas.objBackDummyCmd) {

            m_objGameCanvas.m_objListStartMenu.setSelectedIndex(m_nSelectedIndex,true);
           /* If displayable is Game screen and command is 'Back'
              Set the mode of the Erix to main menu mode*/
            m_objGameCanvas.update(GameCanvas.MENU_SCREEN);
        }

        if (objDisplay == m_objGameCanvas.m_objHighScoreResetForm
                && objCommand == m_objGameCanvas.objYesCmd) {

             m_bHighScoresPresent = false;
             bytHTTPStatus = NOT_FOUND;
             m_bSendMenu = false;
             m_bClearMenu = false;
            /* If displayable is High score list and command is 'Reset'
               Set the mode of the Erix to main menu mode*/
            resetHighScores();

            m_objGameCanvas.update(GameCanvas.HIGH_SCORE_LIST_SCREEN);
        } else if (objDisplay == m_objGameCanvas.m_objHighScoreResetForm
                && (objCommand == m_objGameCanvas.objNoCmd
                || objCommand == m_objGameCanvas.objNoDummyCmd)) {
            m_objGameCanvas.update(GameCanvas.HIGH_SCORE_LIST_SCREEN);
        }
/*
        try{
           	Thread.sleep(250);
        } catch (Exception e) {}
*/
    }


     /**
      *  This method handles the items in the start menu of the game. Start manu can have
      *  'Resume Game' option as its first item depending on whether any saved game is
      *  present in memory or in the RMS.If 'Resume Game' is present then the indexes
      *  of all the items previosly in the menu increases by one,in that case the current
      *  list indexes are to be compared with the 'previous index value plus one'.If
      *  'Resume Game' menu item is not present then normal comparison has to be done to
      *  do the mode switching.First parameter is the current list index and the second
      *  parameter is the value that is to be added to the list index for doing the
      *  comparison.Second parameter can have only two values - zero and one
      *  @param iListIndex current list index.
      *  @param nOption the value that is to be added to the list index for doing the comparison.
      *  @return Returns FAILURE if quit option was selected else 0
     */
    private int handleStartMenu(int iListIndex,int nOption) {

            //int nStatus = 0;
            int nStatus = SUCCESS;
            if (iListIndex == (MENU_SCREEN_START + nOption) ) { // /* If the selected item from the start menu is 'New Game'*/

                /* Set the mode of the Erix to Game mode */
                m_objGameCanvas.update(GameCanvas.START_GAME_SCREEN);
             } else if (iListIndex == MENU_SCREEN_SETTINGS + nOption ) {    // if 'Settings' selected from main menu
                /* Set the mode of the Erix to Settings mode */
                m_objGameCanvas.update(GameCanvas.SETTINGS_SCREEN);
             } else if (iListIndex == MENU_SCREEN_HELP+nOption ) {        // if 'Help' selected from main menu
                /* Set the mode of the Erix to Help mode */
                m_objGameCanvas.update(GameCanvas.HELP_SCREEN);
             } else if (iListIndex == MENU_SCREEN_HIGH_SCORES +nOption) {     // if 'High Scores' selected from main menu

               /* Keeping the player name and the high score, to be used if player selects to
                  send the score to high score server
               */
               m_strHighScoreName = m_arrHighScoreNames[0].toString();
               m_nHighScore = m_arrHighScores[0];
               m_bytHighScoreLevel = m_arrLevelForHighScores[0];

                 /* Set the mode of the Erix to High Score list mode */
//                 try{Thread.sleep(100);} catch (Exception e) {}
                 m_objGameCanvas.update(GameCanvas.HIGH_SCORE_LIST_SCREEN);
             } else if (iListIndex == MENU_SCREEN_QUIT + nOption ) {    // if 'Quit' selected from main menu
                m_bExitMidlet = true;
                nStatus = FAILURE;
             }
        return nStatus;
    }


    /**
      Retrieves the application settings from the RMS. Depending on the return value of this method
      it is decided at the time of application loading that whether the language selection screen
      is to be displayed immediately after splash screen or not.The preferences data for the language
      and the audio is stored as two consecutive bytes with the first byte the language bytes
      and next byte as audio preference byte.

     @return boolean Returns true if data for settings is present.
     */
	private boolean retrieveSettings(){

	   // This zero value indicates not present
	   boolean bIsSettingsDataPresent = false;
	   try {
			// Open the Preferences record Store
			m_objErixPrefStore = RecordStore.openRecordStore("Preferences", true);
			/* Check if records exist */
			if (m_objErixPrefStore.getNumRecords() == 0) {
				bIsSettingsDataPresent = false;
			} else {
				// Get the preferences data i.e first byte for language data + next byte for audio data
				byte[] arrSettingsData = m_objErixPrefStore.getRecord(1);

				// Keeping the first byte as the language data
				m_nGameLanguage  = (int) arrSettingsData[0] ;

				// Keeping the first byte as the audio data
				m_nAudioSetting = (int) arrSettingsData[1];

				//nIsSettingsDataPresent = 1; // indicates record for setting present
				bIsSettingsDataPresent = true; // indicates record for setting present
			}

			if(m_objErixPrefStore != null) {
				m_objErixPrefStore.closeRecordStore();
			}

		} catch (RecordStoreException objRecException) {
			 System.out.println("Exception :: retrieve Settings " + objRecException);
			 /* If Application fails to retrieve the settings for the saved game,it will
				prompt the user for doing the language setting*/
			 bIsSettingsDataPresent = false;
        }

        return bIsSettingsDataPresent;
    }


    /**
     Saves the application settings into the RMS. It Checks if there are no records stored,
     which indicates that user has started game for the first time.In this case, user selected
     settings are added in store.Else they are updated with the existing settings.This method
     adds or updates both the language and audio setting in rms.The first byte is
     language preference data and the next byte is for audio preference data.Each record
     is of two byte.
     @return Returns SUCCESS if the saving was completed successfully.
     */
    private int saveSettings(byte bytLanguageData,byte bytAudioData){
		//int nStatus = 0; // Zero Indicates data saved successfully
		int nStatus = SUCCESS; // Zero Indicates data saved successfully
		try {
			m_objErixPrefStore = RecordStore.openRecordStore("Preferences", true);

			/* Language selection screen can be shown in two conditions.First when
			 * application is launched for the first time nIndex.e there are no language
			 * setting, and second time when user selects 'Settings' menu from the
			 * Main Menu.
			 */

			if (m_objErixPrefStore.getNumRecords() == 0) {
				byte[] arrSettingsData = new byte[2];
				arrSettingsData[0] = bytLanguageData;
				arrSettingsData[1] = bytAudioData;
				m_objErixPrefStore.addRecord(arrSettingsData,0,arrSettingsData.length);
			} else {
				// Update the language and audio preference data:
				byte[] arrSettingsData = new byte[2];
				arrSettingsData[0] = (byte) (bytLanguageData);
				arrSettingsData[1] = (byte) (bytAudioData);
				byte[] arrSettingsRMSData = m_objErixPrefStore.getRecord(1);
				arrSettingsRMSData[0] = arrSettingsData[0];
				arrSettingsRMSData[1] = arrSettingsData[1];
				m_objErixPrefStore.setRecord(1,arrSettingsRMSData,0,arrSettingsRMSData.length);
			}

			if(m_objErixPrefStore != null) {
				// Close the record store
				m_objErixPrefStore.closeRecordStore();
			}

		} catch (RecordStoreException objRecException) {
			//nStatus = -1;
			nStatus = FAILURE;
		}

        return nStatus;
    }


    /**
     Retrieves the list of high scores from the RMS.
     @return Returns SUCESS if the function was completed successfully
             else  FAILURE is returned.
     */
    private boolean retrieveHighScores() {
        // Code to indicate whether data retrieved successfully
        boolean nHighScoresPresent = false;

        try {
            // Open the record store that holds the data for all the high scores
            m_objErixHighScoreStore = RecordStore.openRecordStore("HighScores", true);

            // Check if there are any high scores present
            if (m_objErixHighScoreStore.getNumRecords() == 0) {
                // if no high scores present then initialise the member array variables that
                // that keeps the High scoes and the corresponding name for them.
                for (int nIndex=0;nIndex<NO_OF_HIGH_SCORE;nIndex++ ) {
                    m_arrHighScoreNames[nIndex] = new StringBuffer("   "); //Array of StringBuffer
                    m_arrHighScores[nIndex] = 0;        //Integer array of Scores
                    m_arrLevelForHighScores[nIndex] = 0;
                    m_arrDiffLevelHighScores[nIndex] = 0;
                }

            } else {

                // Getting no of Record for high score and Name present in record store
                int nNoOfRecords = m_objErixHighScoreStore.getNumRecords();

                // Intialising the member array that keeps the high score during game play
                m_arrHighScoreNames = new StringBuffer[nNoOfRecords];

                // Calculate the total of all highscores stored in the RMS.
                // Even though there may be records in the highscores database,
                // it doesn't mean that there are highscores present in it. To
                // differentiate between the two cases, we calculate the
                // total of all the highscores present. If the total is
                // zero, it means all the highscores records are blank, i.e.,
                // there are no highscores present.
                int nHighScoresRunningTotal = 0;

                // Getting all the records from the RMS
                for (int nIndex=0; nIndex < nNoOfRecords; nIndex++ ) {
                    // Since the record ids in record store start from one
                    byte[] arrHighScoreData = m_objErixHighScoreStore.getRecord(nIndex+1);

                    // Getting first eight bytes from the record array and converting
                    // it to a single long value,which is the score.
                    m_arrHighScores[nIndex] = (((long)arrHighScoreData[0] & 0xFF) << 56) |
                                              (((long)arrHighScoreData[1] & 0xFF) << 48) |
                                              (((long)arrHighScoreData[2] & 0xFF) << 40) |
                                              (((long)arrHighScoreData[3] & 0xFF) << 32) |
                                              (((long)arrHighScoreData[4] & 0xFF) << 24) |
                                              (((long)arrHighScoreData[5] & 0xFF) << 16) |
                                              (((long)arrHighScoreData[6] & 0xFF) << 8) |
                                              ((long)arrHighScoreData[7] & 0xFF);

                    nHighScoresRunningTotal += m_arrHighScores[nIndex];

                    // Getting the byte data for the level
                    m_arrLevelForHighScores[nIndex] = arrHighScoreData[8];

      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -