42 videos
238 minutes
We are back with the code once again. I've done clean, and I have taken quite a few things, such as images, because I am going to focus on another aspect important is the entrance, because nothing good is a game if it can be controlled by the player. At least you can play with mobile or desktop you can touch any key on the keyboard or use the mouse. As we are focused on Android, I'll give more priority to whatever
work with Android, and Android the main device will be the finger, which will allow us to tap the touch screen. There are more ways to control a game in Android. We have physical keys if the phone has physical keys; or the volume keys, or some special phones have a physical keyboard which can be controlled with keyboard, but mainly will focus on fingers. We have two ways of working with input. libgdx in his famous class Gdx
It has a complex system of input: input, through which we can work with different things from the entrance. For example, check if the screen it is being played or just be touched. If we played the accelerometer, how the phone is rotated, rotation, where the mouse is ... a lot of things, even vibrate the phone, simply through Input. Obviously if we try to vibrate the computer
You will not get to do anything. But it is an example of input lets us do all kinds of things. And I'm going to focus on touching the screen. As test the we can do on a computer, on a PC instead of finger have a mouse, but it is exactly the same. For example, we can check if the screen It is being played with IsTouched (). Returns true if the screen is being pressed. I can do that if you are touching the screen print the message
You're touching the screen.' When I run this, if I touch the screen at the moment the terminal is filled with messages that say I'm touching the screen. Why so many? Because as I said at the beginning of time, render runs 60 times per second, so it is normal to be printed as many times as checking makes a lot of times. But for example, instead of IsTouched () we can ask whether just been touched, with justTouched ().
This will return true once, but until you touch the screen again not again return true. It is much more convenient if we detect if you have touched the screen when we want to make the jump. If you touched the screen is printed. But does not print a lot of times, only once. If I lift your finger mouse and I again press reappears. But not a lot of times. Now if because I am pressing myself. For completing this,
although I do not want to focus on desktop, obviously if we have a physical keyboard on the phone or desktop we have the isKeyPressed () method to see if we have pressed a key. Here we use as parameter the key you want to check if it is pressed. These keys are stored in a special class, Input.Keys, with all the keys are in the Keys class. Not only PC keys. Besides keys PC
we have the key behind a mobile, or the HOME key or the volume keys ... some a little curious as multimedia keys or key L1, L2, R1, R2 ... no official support for Play Station, so I do not know what they are ... maybe for the console that has Sony, the PSP with Android, I do not remember the name. I imagine that it exists, but it is curious in general the amount of things we can do with keys.
Yet all are characterized, as we check in if () provided, but do nothing, let's open the game and leave him still, always you are running the IF and resources are being used to check if you are touching the screen. That it is not being pressed, but you have to make the IF. So libgdx Input beyond what you have is a better alternative to check if you are touching the screen
or if you are pressing a key, which is used processors, which is an asynchronous system and much more functional than ask questions.
About the Author
Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.