Programming Games for Android

Programming Games for Android 07: Insert images with SpriteBatch

42 videos

238 minutes

To display a screen image we have to use the render method, as I said earlier. To do what we have to do is this. First we tell the Batch, the kind we use to draw things on the screen, we want to start drawing things, simply calling the begin method. Then we put everything we want to draw on the screen, we'll see how it is done, and finally we say we're done with a call to end.

And when we call end, everything we have said between begin and end so suddenly drawn to make it faster. Within Batch, there is a very important method called draw, which serves to draw things. As you see, we have lots of ways to call draw from short methods to long methods like this that impresses the number of parameters you have. We also have some methods that are textures,

and other receiving regions, we'll see what that is. In this case I want to use the shorter method, which is receiving a texture, and the X, Y positions where we want to draw on screen. But you see, there are other ways to call draw. You can tell the size, as width or height. You can say what you want to scale you have, what rotation should have, if you want to be rotated, or a flip. That is to say,

you have plenty of ways to tell you how to draw, but in this case I want the shorter to get results as soon as possible. I'm going to ask you draw minijoe position 0, 0. You have chosen these numbers is no accident, I will you now understand why. If I run this code on your desktop (I already told you that we would use the desktop for convenience), you see that happening in the first place a very rare thing.

This is because before recording this video I deliberately made a test and have shown images at different points screen. I'll close this before burn my screen. It is important that before drawing any frame buffer clean up the video card. Why? Because I've done the test deliberately removing dispose, ie, that the resources have remained on the graphics card and have not been released.

It has been the buffer video card dirty and when I asked him to draw has seen the ghost of what had before in my previous test image. You do not want that to happen, so you have to solve this in two ways. A dispose of doing what you want to stop using this does not happen, and another is to clear the screen before drawing things in it. For this it is very simple to do only a call to Gdx.gl. gdx

It is an interesting class, and I will speak in detail later, this is the one we use to control the game, graphics, entry ... in this case, OpenGL, which is what is below the graphics card, and asking him to empty. What is emptied what? The bit buffer, COLOR_BUFFER_BIT writing. You can copy and paste this code, do not give much back, but it is important to ask the graphics card

Empty all colors you have on screen.' As we do this, it will be like silk and will be perfectly fine. Because if we do not ask him the screen is empty, it can be dirty and it may happen that. You see why it is important also free up resources, not to pass it. As you can see, the image is shown at 0.0 below on the left. That's because libgdx has an axis of coordinates interesting

which it is important to talk. To libgdx all pixels of the screen have a position, undoubtedly. The lower left, the bottom right. Pixels as any screen. In particular, libgdx has the coordinate axis starting at 0.0, which is located down left. The X grow to the right and the Y grow up. That is, if I ask you the image instead of the draw at 0.0

in 200.0, as I say X = 200, the image will be further to the right than before, for example, such that here. Similarly, if instead of saying 0 I say 200 vertically, the going to see more up than usual. For example, in this case rise here. The higher you look older It must be the value of the Y, and the further to the right, the greater must be the value of X. How can guess how tall my screen?

Let's take a look at the Gdx class again, and this is what we asked the graphics system. The graphics system has a number of interesting methods detailed later, and one of them is getWidth, used to obtain the width of the screen, and getHeight, to get the top of the screen. For example, if I come to create and I am writing this code, [drums], I know how big is the screen

in which the game is running. And this could save variables, and use it for what interests us, for example, I can keep this in a variable called width, and this in a variable called height.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

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.