42 videos
238 minutes
Let's start by displaying images on the screen, as I said. What I have done is that the Android project have gone to the assets folder, this folder already told you about earlier when I presented the way we create projects. And I left this I'll teach image. I have taken this, that maybe you sounds because it is what I taught you when we saw the project in Chapter 1. In the game more or less finished image.
And I left here. You do the same with the image you like. If you want to use a pon one, if you want to use another put another. But the important thing is to put it and remember the name. Let's see how you can represent on screen and how to see it in the game. To do what I have to do is create an instance a class called Texture. 'Why Texture, whether it is an image?' Thus in libgdx are called to images, texture,
because it is the internal name using graphics cards for these things. Graphics cards, it seems not, but they are complicated devices they have to not only draw pixels, polygons must prepare, prepare vertices and make some complex things that do not interest, It's why we use libgdx, but libgdx as you know that inside he called Textures calls just as well without any problem.
As I am a person with little imagination in my image I'll call the 'minijoe' because it really is the name of the image. Now I import and this is important as you see there are two classes that are called Texture. Whenever in doubt, matters which begins com.badlogic.gdx, because this is the texture libgdx. If you import one that is not to libgdx you probably have problems, so make sure, in case of doubt,
what are importing a class that is part of the package: com.badlogic.gdx. Once ready, I can do two things. One, charge it, and two, represent. That is why I have set as a field, because I want to use it to charge both create and render to render it. I'll go first to the part load, and it is quite simple. I use the builder Texture. You have lots of ways to call the constructor.
I'm interested in the simplest of all, which only provides a route as a string of characters, such as minijoe.png, which is the name my image. If you called pepito.png, put pepito.png. If you are in a subfolder, put the name of the subfolder, bar, as llame.jpg. I recommend PNG because it looks good and weighs little compared to JPG because JPG is then blurred, it does not look good. Always use PNG if at all possible.
Once I have it, I have to represent. Sure there is a method to represent things, right? Well, no. Because represent a texture it is more complicated than it seems. Although not notice, because you say 'I have a powerful mobile, no note'. You have to give a series of commands the graphics card that make losing performance, then usually when you draw textures do not want to draw them one to one, but draw them away,
because it is proven that represent a texture on the screen takes the same time to represent 50, which is quite in terms of microseconds. So instead of simply asking 'show this', 'then this', 'then this' with libgdx a structure used to say 'used to see, since it will take we do one thing: say what you want when you're ready to draw and draw it all at once 'And that's probably faster to tell someone..
It is nonsense, they are microseconds'. But when you have tens of textures loaded this will be a difference. This kind of which I speak is called SpriteBatch, and as its name suggests working with textures batch, which is as we would call in Spanish. This class is asked to draw this, this, and that. And when you are ready, send commands coup and takes much less. Like the texture, he should be starting in the create method.
And one thing I have not spoken but it is important, is the issue of releasing resources. If you do not release these resources when we close the game, they will stay on the graphics card, especially the texture, and that is not because the game wants to contaminate the video memory and performance will the phone down. So please, please, when you stop using something, it releases its resources. To free resources there is a method called dispose,
which is another method ApplicationAdapter, and the last of all runs when it will close the game. Here you must release all resources they have been used to not stay lost wandering in the video memory or memory. In the dispose method we will release the texture and SpriteBatch. Simply we call the dispose method and the method of minijoe dispose of SpriteBatch. You see, as things are releasable,
They have a call to dispose. Whatever is releasable you have a call to dispose. Some things are not releasable because they eliminate themselves, if you are pure and Java. That will not have to release. But everything is disposable, it should be free. Because you do not want to annoy performance phone or computer performance because of textures that have been there. And now, to draw this screen.
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.