Skip to content

Instantly share code, notes, and snippets.

@mrp1q7z
Created September 23, 2014 11:43
Show Gist options
  • Save mrp1q7z/67c8e0c308098187daae to your computer and use it in GitHub Desktop.
Save mrp1q7z/67c8e0c308098187daae to your computer and use it in GitHub Desktop.
libGDX sample
package name.taoka.ugeee.inputdemo;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GdxMain extends ApplicationAdapter {
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("img01.png"));
sprite = new Sprite(texture);
sprite.setPosition(w / 2 - sprite.getWidth() / 2,
h / 2 - sprite.getHeight() / 2);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
sprite.setPosition(
Gdx.input.getX() - sprite.getWidth() / 2,
Gdx.graphics.getHeight() - Gdx.input.getY()
- sprite.getHeight() / 2);
}
if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) {
sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()
/ 2, Gdx.graphics.getHeight() / 2 - sprite.getHeight() / 2);
}
batch.begin();
sprite.draw(batch);
batch.end();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment