This is the 8th day of my participation in Gwen Challenge
preface
In the first four chapters (juejin.cn/post/696995…) , (juejin. Cn/post / 697032…). , (juejin. Cn/post / 697068…). , (juejin. Cn/post / 697107…). In this chapter, we introduce how to build the libGDX development environment and how to run the project, as well as the functions of the modules in the project. We have a basic understanding of libGDX, and have learned about libGDX resource management, graphics rendering, and sound playback apis by loading the resource section and graphics rendering section. We’ll walk through the code to see how libGDX listens for screen touches, mouse and keyboard input.
In order to more clearly describe the functions of each API, we will use a large number of codes and descriptions to explain the functions and uses of each API. Therefore, we will divide the development of small games into four parts. Due to space constraints, we will divide it into three chapters to explain the main contents of each part.
- Loading resources
- Graphics rendering, screen clearing and sound playback
- Mouse keyboard input and movement
- Small game complete summary
This section is about mouse and keyboard input and movement
Make buckets move (listen for screen touches and mouse)
It’s time to give the user control of the bucket. I said we’re going to allow users to drag buckets. Now to make things a little easier, if the user touches the screen (or presses the mouse button), we want the bucket to be horizontally centered around that position. Adding the following code to the bottom of the Render () method does this:
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 64 / 2;
}
Copy the code
First, we ask if the current user touched the screen (or pressed the mouse button) by calling the gdx.input.istouched () method of the input module. Next, we need to convert the touch/mouse coordinates to camera coordinates, which is necessary because the touch/mouse coordinates are different from the actual coordinates of objects in our game world.
Gdx.input.getx () and gdx.input.gety () return the current touch/mouse position (libGDX also supports multi-touch, but that’s the subject of another article). To convert these coordinates to our camera coordinate system, we need to call the camera.unproject() method, which asks Vector3 for a three-dimensional vector. Create such a vector, set the current touch/mouse coordinates and call the method. This vector will now contain the touch/mouse coordinates of the bucket’s coordinate system. Finally, we changed the bucket’s position to be centered around touch/mouse coordinates.
Note: Instantiating many new objects (e.g., Vector3 instances) is very, very bad. The reason for this is that the garbage collector must be started frequently to collect these short-lived objects. While this is not a big deal on a PC desktop (because PC performance is better), on Android GC can cause pauses of hundreds of milliseconds, resulting in stuttering. To solve this problem in this particular case, we can simply create a Drop field for the class touchPos instead of instantiating it all the time.
Note: touchPos is a THREE-DIMENSIONAL vector. You may be wondering why we only operate in 2D. OrthographicCamera is actually a 3D camera that also takes z coordinates into account. Think of CAD applications that also use 3D orthogonal cameras. We just abuse it to draw 2D graphics.
Make buckets move (keyboard)
On the PC desktop and in the browser, we can also receive keyboard input. Let’s move the bucket when we press the left or right cursor key.
We want the bucket to move left or right at 200 pixels per second without acceleration. To achieve this time-based movement, we need to know how much time has passed between the last frame and the current render frame. Here’s how we did it:
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
Copy the code
The method gdx.input.isKeyPressed () tells us if a particular key is pressed. The Keys enumeration contains all key codes that are supported by libGDX. The etf has. Graphics. GetDeltaTime (in seconds) between the returns on a frame and current frame after a time. All we need to do is change the bucket’s x coordinate by adding/subtracting 200 units times the incremental time in seconds.
We also need to make sure the bucket stays within the screen limit:
if(bucket.x < 0) bucket.x = 0;
if(bucket.x > 800 - 64) bucket.x = 800 - 64;
Copy the code
Pause and resume in the game
Android pauses and resumes your application every time a user answers a phone call or presses the home button. In this case, libGDX automatically does a lot of things for you, such as reloading images that might be missing (OpenGL context loss is a terrible topic in itself), pausing and resuming music streams, and so on.
In our game, there is no real need to deal with pause/resume. Once the user returns to the application, the game picks up where it left off. Usually achieve a moratorium on the screen and requires the user to touch the screen to continue, you can view ApplicationAdapter. Pause () and ApplicationAdapter. Resume () method.
conclusion
So far, our small game is finished, the next chapter we will summarize the previous several chapters of the code, complete the complete game code.
If you feel that the blogger wrote a good, welcome to “like, favorites, attention” three links.