Created
September 23, 2014 12:21
-
-
Save mrp1q7z/dc285e72d8ea1cf26f7e to your computer and use it in GitHub Desktop.
libGDX sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package name.taoka.ugeee.inputdemo; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Input.Buttons; | |
import com.badlogic.gdx.Input.Keys; | |
import com.badlogic.gdx.InputProcessor; | |
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 implements InputProcessor { | |
private SpriteBatch batch; | |
private Texture texture; | |
private Sprite sprite; | |
private float posX, posY; | |
@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); | |
posX = w / 2 - sprite.getWidth() / 2; | |
posY = h / 2 - sprite.getHeight() / 2; | |
sprite.setPosition(posX, posY); | |
Gdx.input.setInputProcessor(this); | |
} | |
@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); | |
sprite.setPosition(posX, posY); | |
batch.begin(); | |
sprite.draw(batch); | |
batch.end(); | |
} | |
@Override | |
public boolean keyDown(int keycode) { | |
float moveAmount = 1.0f; | |
if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) { | |
moveAmount = 10.0f; | |
} | |
if (keycode == Keys.LEFT) { | |
posX -= moveAmount; | |
} | |
if (keycode == Keys.RIGHT) { | |
posX += moveAmount; | |
} | |
return true; | |
} | |
@Override | |
public boolean keyUp(int keycode) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean keyTyped(char character) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean touchDown(int screenX, int screenY, int pointer, int button) { | |
if (button == Buttons.LEFT) { | |
posX = screenX - sprite.getWidth() / 2; | |
posY = Gdx.graphics.getHeight() - screenY - sprite.getHeight() / 2; | |
} | |
if (button == Buttons.RIGHT) { | |
posX = Gdx.graphics.getWidth() / 2 - sprite.getWidth() / 2; | |
posY = Gdx.graphics.getHeight() / 2 - sprite.getHeight() / 2; | |
} | |
return false; | |
} | |
@Override | |
public boolean touchUp(int screenX, int screenY, int pointer, int button) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean touchDragged(int screenX, int screenY, int pointer) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean mouseMoved(int screenX, int screenY) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean scrolled(int amount) { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment