Game Development Reference
In-Depth Information
The dispose() method
The
dispose()
method is the place where you clean up and free all resources
that are still in use by an application, as shown here:
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
There is an interface called
Disposable
that is implemented by every LibGDX
class that allocates resources (that is, memory) and can be easily deallocated by
calling the corresponding
dispose
method. In the preceding code, this is done
for the sprite batch and the loaded texture.
The following is a complete listing of the
MyDemo.java
source file from the
demo
project:
package com.packtpub.libgdx.demo;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class MyDemo implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
@Override
public void create() {
float w = Gdx.graphics.getWidth();