How can I use Android to call update?

Source: Internet
Author: User
Tags time 0

The following describes in detail how to call update in Android and how to work in detail. When the sleep time is reached, call update first to prepare for the next draw, let yourself sleep, and finally notify the system to redraw yourself.

Then, after waking up, the Handler will allow the system area to draw a map of the last modified two-dimensional blocks, and then Android will call update again, so that the cycle repeats and continues to grow ), so that the game is constantly pushed forward. Therefore, it is better to be an "engine.

Since update is the driving force of the game, it is enough to stop the game as long as Android no longer calls update, because the screen is actually static ), therefore, when the game is paused, it can be switched to "running". In fact, it can be modified and then drawn)

 
 
  1. public void update()  
  2.     {  
  3.         if (mMode == RUNNING)  
  4.         {  
  5.             long now = System.currentTimeMillis();  
  6.             if (now - mLastMove > mMoveDelay)   
  7.             {  
  8.                 clearTiles();  
  9.                 updateWalls();  
  10.                 updateSnake();  
  11.                 updateApples();  
  12.                 mLastMove = now;  
  13.             }  
  14.             mRedrawHandler.sleep(mMoveDelay);  
  15.         } 

If the entry fails, the two-dimensional square map remains at the last screen, which is why the whole map should be cleared at the beginning.) After the game fails, start the new game again. At this time, you can observe the underlying data of the last game running by setting the breakpoint ].

But I personally think that the code below Snake is a bit strange to read, a bit like a "first chicken, or first eggs? "The problem leads to a" Strange Circle "in my thinking logic.

 
 
  1. switch (item.getItemId()) {  
  2.          case MENU_ITEM_DELETE: {  
  3.              // Delete the note that the context menu is for  
  4.              Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);  
  5.              getContentResolver().delete(noteUri, null, null);  
  6.              return true;  
  7.          }  
  8.      }  
  9.      return false;  

According to the meaning of this Code, when the sleep time is reached, first go to Android to call update, that is, to prepare for the next draw, then let yourself sleep, and finally notify the system to re-draw yourself. Well, it's hard for me to understand. Back to the example of time 0, let the snake go north at time 0, referring to the modification of the Two-dimensional square map at the bottom layer, not the interface ).

Then let yourself sleep for 0.6 milliseconds. When the time is up, first go to Android to call the update method, and then let the snake body make changes, that is to say, it is not in vain to overwrite the last modification that has not been drawn yet ).

What's more, in update, we will let ourselves sleep. We haven't called invalidate. How can we sleep again ?), How can I notify the system to call my onDraw method? That is to say, invalidate is not executed at all ??? In my understanding, the order should be reversed. First, the system should be notified to call the onDraw Method for re-painting, so that the last modification to the two-dimensional square map at the underlying layer will be displayed.

Then prepare for the next modification, and finally let yourself sleep, waiting for waking up, so that the cycle repeats. The experiment proves that it is correct to turn it upside down, but I hope some friends can give me some advice on this confusing place! I remember that when setInterval is used in javascript, I first write the processing logic and then write a setInterval statement at the end. This is also my habit of thinking.) Do you have any idea about this method on google?

In addition, it is a waste of time to re-draw the walls every time, because the walls do not change at all. In addition, the original intention of setting the mLastMove variable is to ensure that the previous change from the current time point has passed mMoveDelay in milliseconds. However, since the sleep mechanism has been used, it seems unnecessary to use this time difference.

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.