[First contact with Android] a question about Google sample lunarlander

Source: Internet
Author: User
Tags home screen android games

In other words, it's okay to play Android games. A problem was found.

There is a lunarlander example in Google's android-sdk-windows-1.0_r2 sample. A very interesting game, very difficult. However, one problem found was that I did not introduce how to play the game. I never knew how to control the landing. After reading the code, I know that holding down S is the firing (depend, this year should not talk about fire !). This allows you to play this game.

 

So I came up with an idea. In this example, add a help menu to bring up the Help button. This is also the first time I have been familiar with Java and eclipse learning and practice.

 

1. Like VC, you must first create a menu resource and string for help.
Add a row to the oncreateoptionsmenu (menu) of lunarlander. java.

 Public Boolean oncreateoptionsmenu (menu) {<br/> super. oncreateoptionsmenu (menu); </P> <p> menu. add (0, menu_start, 0, R. string. menu_start); <br/> menu. add (0, menu_stop, 0, R. string. menu_stop); <br/> menu. add (0, menu_pause, 0, R. string. menu_pause); <br/> menu. add (0, menu_resume, 0, R. string. menu_resume); <br/> menu. add (0, menu_easy, 0, R. string. menu_easy); <br/> menu. add (0, menu_medium, 0, R. string. menu_medium); <br/> menu. add (0, menu_hard, 0, R. string. menu_hard); <br/> menu. add (0, menu_help, 0, R. string. menu_help); // added the Help button </P> <p> return true; <br/>}

2. Double-click eclipse> package explorer> res of the lunarlander project> string. xml under the values folder to add
"Add..." adds a string to display the menu content.
Name: menu_help
Value: Help.

In addition, you need to add a string to display the Help content.
Name: message_help_content
Value: "Left arraw key: Counter-clock rotate/nright arraw key: clock rotate/NUP arraw key: start/stop/nblank key: Firing/ns key: restart"

3. Add the switch branch to the onoptionsitemselected function of lunarlander. java.
Public Boolean onoptionsitemselected (menuitem item) {<br/> switch (item. getitemid () {<br/> case menu_start: <br/> mlunarread. dostart (); <br/> return true; <br/> case menu_stop: <br/> mlunarread. setstate (lunarread. state_lose, <br/> gettext (R. string. message_stopped); <br/> return true; <br/> case menu_pause: <br/> mlunarread. pause (); <br/> return true; <br/> case menu_resume: <br/> mlunarread. unpause (); <br/> return true; <br/> case menu_easy: <br/> mlunarthur read. setdifficulty (lunarread. difficulty_easy); <br/> return true; <br/> case menu_medium: <br/> mlunarread. setdifficulty (lunarread. difficulty_medium); <br/> return true; <br/> case menu_hard: <br/> mlunarread. setdifficulty (lunarread. difficulty_hard); <br/> return true; <br/> case menu_help: // added the Help button <br/> help (); <br/> return true; <br/>}</P> <p> return false; <br/>}

4. Add the help function in lunarlander. Java:
Public void help () {<br/> new android. app. alertdialog. builder (this) <br/>. settitle ("help") <br/>. setmessage (R. string. message_help_content) <br/>. seticon (R. drawable. app_lunar_lander) <br/>. setnegativebutton ("OK", <br/> New dialoginterface. onclicklistener () {<br/> Public void onclick (dialoginterface dialog, int whichbutton) {<br/> // the dialog box is open, and then press home, when you start lunarlander again in the program, the program reports an error. <Br/> // solution, set Builder (lunarlander. this) is changed to Builder (this) <br/>}< br/>}) <br/>. show (); <br/>}

This Nb code was not written by Google.
Nnd, a simple MessageBox message box in VC, this is actually so complicated.
The concept of set is used (this concept has been learned before using JavaScript ).

5. Now you can perform the test. Select a project and click Run. It runs on the simulator.
Click menu> More> help on the simulator. Help.

 

6. In an episode, perform a test again. You will find a bug, that is, when the help dialog box is maintained, press the Home Key to return to the home screen, and then enter the lunarlander program again, the system will report an error.
"Sorry! The application Lunar Lander (process com. example. Android. lunarlander) has stopped unexpectedly. Please try again. Force close "e. g:

7. How can I modify it? Who knows to tell me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.