Android Multi-threaded Hamster game

Source: Internet
Author: User

  Foreword: Recently compare like multithreading, see some thread case, here summarizes the whole process of playing the hamster game.

1. First, the main activity to load the layout, I generally like to write this earlier, so that in the main activity to play with these controls. Gossip not to say, a framelayout, has been what I used is what relativelayout,linearlayout, in fact, this thing would like to make a layout, only that. Inside there is a imageview, mainly is the mouse picture.

2. Next, declare some variables or constants in Mainactivity, which are temporary-----get things from others and use them yourself. The main member variables are the number of mice, the ImageView object, the handler object, the mouse position of the two-dollar array, which is the coordinates on the screen.

3. Open a thread directly in the OnCreate. Thread T=new thread (new Runnable) {This contains a run method}-----The Run method has an index value that records the mouse position index, and then a while (! Thread.CurrentThread (). isinterrupted ()) {.... Generates a random number-----gets a message (using the handler defined later)-----Some identification of the message-----handler.sendmessage (m) Send a message to handler-----then the thread sleeps for a while-----is also random}

4. Continue fencing in OnCreate, Handler=new handler () {Duplicate a handlermessage (Message) { If it's the MSG from the front-----get the location index value-----Set the x-coordinate-----Set the y-coordinate-----Set the mouse to display-----such as a imageview.setvisibility (view.visible)}----- ImageView is the mouse's picture name}

5. Add a touch listener to the mouse-----first let it disappear-----then the number of mice hit by the user +1. Mouse. Setontouchlistener (new Ontouchlistener () {...}) -----Make a copy of the Ontouch method, the parameter is a view and an event, which will be the view. Setvisibility (view.invisible), then the global i++, and then a toast is displayed to the user.

6.That's all. It was so simple to hit the hamster. is the simple multithreading interaction.

Good Morning (づ ̄3 ̄) Old-----2017/7/14 8:00

Android Multi-threaded Hamster game

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.