so i’m making a game and all i have done is the texture of the main menu,but i ran into a problem.when ever i run the game and i wait a couple if seconds and then my RAM goes all the way up to 99% use in around a minute.i can’t continue the game unless i fix this.i’m using Java (of course),eclipse,and libGDX.Does anyone know how i can fix this?
Look for a loop somewhere that’s creating a bunch of objects. You may accidentally not be exiting a loop, therefore it’s making a bunch of things. Also, you might have something in an update method that should be in an initialization method.
should i add my code so u can see if there is anything wrong?I don’t think there is a loop but it can be a bunch of things…I think this is a update method-
camera.update();
if it is how would i change it?Also how could i find a loop…Also sorry i’m kind of newbie at this…
Do you keep loading textures or creating new objects in the render method?
is this what your talking about?-
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1F,1F,1F,1F);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
camera.update(true);
batch.setProjectionMatrix(camera.combined);{
Texture texture_back;
texture_back = new Texture(Gdx.files.internal("menu/back.png"));
batch.begin();
batch.draw(texture_back, 0, 0);
batch.end();
Yes, that texture_back object is being created every frame, which you do not want to do. Keep the texture object in global space and then create the texture in the create method. I also recommend you learn the basics of LibGDX and how rendering works. Also learn about objects in Java and how they are created.
Yep. I’ve highlighted the problem line. To fix this, put the highlighted line in the [icode]create()[/icode] method, and make the [icode]texture_back[/icode] a global variable by putting it outside of any methods. (Right above the create method is a good place)
Edit: What opiop said
Texture texture_back;
@Override
public void create() {
texture_back = new Texture(Gdx.files.internal("menu/back.png"));
}
it created a error where the texture_back is-
batch.begin();
batch.draw(texture_back, 0, 0);
batch.end();
What error did it create? A nullpointer?
You have a lonely opening brace after [icode]batch.setProjectionMatrix(camera.combined);[/icode]
Edit: I’m not sure if this is causing the problem, but you should post the stack trace so we can make sure.
here’s the whole method with the error at texture_back-
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1F,1F,1F,1F);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
camera.update(true);
batch.setProjectionMatrix(camera.combined);{
batch.begin();
batch.draw(texture_back, 0, 0);
batch.end();
}
}
it says it can’t be resolved to a variable.
edit-the create method is also in the game class not the Main or the GameScreen class
Oh whoops! Are you extending the screen class? You would then put the code in the init method in the same class as this render method.
Or you could through the code into the constructor for the class.
should i just show u all of my classes done which is only 3.the game class is extending but the GameScreen class is not.
Just put the code in the constructor of this class like opiop suggested.
I really can’t stress this enough: don’t start programming straight off with making games and third party frameworks. I know game development is often appealing, but it’s one of the more involved divisions of programming, involving aspects from all over the place.
Sure, we can help you with this one issue, but you will only keep having problems if you don’t know the fundamentals. You’ll only frustrate yourself as it’s like trying to do algebra without knowing about the concept of arithmetic.
I recommend starting out with something like Code Academy, and maybe picking up an actual book or two. I’ve yet to find internet tutorials that can hold a candle to a good book. A good book will walk you through the basics, then have you write simple programs, bringing up your capabilities at the same pace as your knowledge, unlike diving off the deep end of game dev without knowing how to swim.
i’m sorry…I’m only 13 and all i really looked at where videos that showed me very basic stuff…I’ve started learning this about 2 weeks ago and i just thought it would be cool to know stuff like this.I might try the Code Academy considering that it is free.
Don’t be sorry. It’s cool you think programming is cool, because it is. But to become good at it you have to ease into it, and start with the basics. Trust me when I say game programming isn’t basic at all, at least at even this level. It terms of games, you should start out with a text-based game that runs in the console. If you can do that, then you’ll have learned quite a bit already. Even more basic than that would be a calculator program, type in numbers, tell it what you want to compute, and it spits out an answer. That’s a sample exercise from one of the books I started with.
so basically add it to its own class as a object?