[Android game development 15th] Notes on Performance Optimization of ontouchevent () Touch Screen events in android game development!

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/336.html

Many kids shoes say that after my code is run, clicking home or back will cause a program exception. If you have encountered this, you certainly haven't carefully read the himi blog, in article 19th, himi specifically wrote about the causes and solutions of these errors. I have added my remarks on this blog, and my provincial children's shoes are always confused. Please click here to contact us for further reading:

[Android game development 19th] (required) surfaceview Running Mechanism explanation-analyze back and home buttons and switch to the background to handle exceptions!


Let's take a look at the previous Code:

Package COM. himi; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. motionevent; <br/> Import android. view. window; <br/> Import android. view. windowmanager; <br/>/** <br/> * @ author himi <br/> */<br/> public class mainactivity extends activity {<br/> private object; <br/> private final int time = 50; // Note 1 <br/> @ override <br/> P Ublic void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); <br/> This. requestwindowfeature (window. feature_no_title); <br/> setcontentview (R. layout. main); <br/> Object = new object (); <br/>}< br/> @ override <br/> Public Boolean ontouchevent (motionevent event) {<Br/> If (event. getaction () = motionevent. action_down) {<br/> log. V ("himi", "action_down"); <br/>} else if (event. getaction () = motionevent. action_up) {<br/> log. V ("himi", "action_up"); <br/>} else if (event. getaction () = motionevent. action_move) {<br/> log. V ("himi", "action_move"); <br/>}< br/> synchronized (object) {// Note 2 <br/> try {<br/> object. wait (time); <br/>} catch (interruptedexception E) {<Br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> return true; // true must be returned here! For the reason, see [9 of android2d Game Development] <br/>}< br/> 

 

The code is very clear.Note 2Location:

 

I. Preface:


All the children's shoes must know that in the simulator, when we click a simulator screen and release it, action_down is triggered first and then action_up; if we move it on the screen, action_move is triggered; OK, right. But you need to know that this is just a simulator !!!!!

 

Ii. Differences between a real machine and a simulator:

When it comes to our small users (when it comes to users, I think of the classic line of "My name is Mt" from the dark night man: Dear customer, I am a tender Dad !) Back to the topic: when our users are playing our games, especially RPG, they must press our virtual buttons on the touch screen for a long time, for example, we will draw a virtual steering wheel on the screen ~ In fact, the action_move event will be continuously responded by Android !!

 

3. Why does action_move always respond? If a user does not move his or her fingers, but does not move his or her fingers, the user will always respond?


There are two reasons: First, Android is very sensitive to touch screen events! Second: although our fingers are still and not moving, this is not the case! When our fingers touch the screen of the mobile phone, we feel that our fingers are still motionless. In fact, our fingers are shaking and shaking. Don't believe you try to hold your fingers at rest? Hey hey ~

 

So ~ We have to analyze it. If action_move has been continuously responded and processed by Android OS at this time, it will undoubtedly put a lot of pressure on our game performance!

For example, if the rendering time of the game thread is 100 ms each time, when you touch the screen with your fingers, about 10 motionevents will be generated in the short 0.1 seconds, in addition, the system will send these events to the listening thread as quickly as possible. In this way, the CPU will be busy processing ontouchevent during this period of time, so that the image will become stuck and stuck.

In fact, we don't need to press the button to respond so many times. Instead, we need to accept the user button after each drawing or before drawing, this will prevent the frame rate from dropping too much, isn't it ?! So ~ We need to control this time, let him slow down, and work together with our drawing time ~ This reduces the burden on many system threads.

 

NOTE 2:

Some children's shoes may ask why the sleep () method is not required. In fact, if we just want to sleep the thread for a specified time, we can use the sleep () function, but there is no resource lock limit. The wait and notify methods of the object are usually used to limit the wait time, and must be written in the synchronous code block. So ~

Some children's shoes will ask why this. Wait () is not used by objects of the current class, but a new object is used:

In synchronized, the object indicates that the wait call of the object must have the monitoring lock of the object. Currently, we have the object lock, so we need to use the object to call Wait ~

Note 1:

We all know that the variables here are actually the sleep time we set, so here I want to tell you the value of this time, as I have mentioned above, in our game, we only need to press the button at the same time as our drawing thread. Of course, this is our ideal time! If there are character frames in the game, it is quite appropriate to set the sleep time based on the number of frames. After all, the logic of one frame of the character is executed ~ This depends on the game situation ~


Note: The object. Wait (long timeout) method also needs to be used with caution!

The reason is that the test found that the sleep time is actually a little longer than the time you specified, but it is okay to properly control the time.

 

 

Supplement:

1. I can see my shoes. // Note 2. Can I use this, that is, the current object? The answer is yes, but it is best not to use it like this, the reason is that a deadlock may occur if the object needs to be synchronized elsewhere! The reason why a new object can be used is that the Code does what it should do and does not affect each other,

2. // Note 2 we can optimize it here. After all, an object is a waste of resources. We only need one byte, SO ~ We can define one as follows: byte [] Lock = new byte [0]; this is the best ~

 


 

 

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.