Slide 62
Slide 62 text
public class TestGame implements ApplicationListener {
private Texture gorbaTexture;
private Rectangle gorbaRectangle;
private Texture phoneTexture;
private Rectangle phoneRectangle;
@Override
public void create() {
...
gorbaTexture = new Texture(Gdx.files.internal("gorba.png"));
gorbaRectangle = new Rectangle(0, 0,
gorbaTexture.getWidth() / 100.0f, // 84 pixels -> 0.84 meters
gorbaTexture.getHeight() / 100.0f); // 74 pixels -> 0.76 meters
phoneTexture = new Texture(Gdx.files.internal("phone.png"));
phoneRectangle = new Rectangle(10.0f / 2.0f, 5.0f / 2.0f,
phoneTexture.getWidth() / 100.0f,
phoneTexture.getHeight() / 100.0f);
}
@Override
public void render() {
...
if(Gdx.input.isTouched()) {
int realX = Gdx.input.getX();
int realY = Gdx.input.getY();
Vector3 touchPos = new Vector3(realX, realY, 0);
camera.unproject(touchPos);
gorbaRectangle.x = touchPos.x;
gorbaRectangle.y = touchPos.y;
}
batch.draw(gorbaTexture,
gorbaRectangle.x,
gorbaRectangle.y,
gorbaRectangle.width,
gorbaRectangle.height);
batch.draw(phoneTexture,
phoneRectangle.x,
phoneRectangle.y,
phoneRectangle.width,
phoneRectangle.height);
if(phoneRectangle.overlaps(gorbaRectangle)) {
soundEffect.play();
phoneRectangle.x = MathUtils.random(0f, 10f);
phoneRectangle.y = MathUtils.random(0f, 5f);
}
}