I have an Actor which has set a drag listener:
ballActor.addListener(new DragListener() {
public void drag(InputEvent event, float x, float y, int pointer) {
ballActor.moveBy(x - ballActor.getWidth() / 2, y - ballActor.getHeight() / 2);
}
});
This actor is added to a stage:
W = Gdx.graphics.getWidth();
H = Gdx.graphics.getHeight();
camera = new OrthographicCamera(W / RATE, H / RATE);
stage = new Stage(new ScreenViewport(camera));
stage.addActor(ballActor);
Gdx.input.setInputProcessor(stage);
Now if I use meter to pixel ratio 1:1 the drag works very well but if i use another ratio, like 1:160 the drag event is not firing.
In resize method I update the camera:
camera.viewportHeight = H/RATE;
camera.viewportWidth = W/RATE;
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
I read that if I use a stage I dont have to translate from screen coordinates to camera coordinates. The actor has set the position, width, height and also bounds. What am I doing wrong?
UPDATE:
Here is the code:
public class Pendulum extends ApplicationAdapter {
private Stage stage;
public static final float RATE = 160f; //with 1f..20f drag works
float W;
float H;
@Override
public void create() {
W = Gdx.graphics.getWidth() / RATE;
H = Gdx.graphics.getHeight() / RATE;
Texture ballTexture = new Texture(Gdx.files.internal("ball.png"), true);
ballTexture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
TextureRegion ballRegion = new TextureRegion(ballTexture);
final Image image = new Image(ballRegion);
image.setSize(image.getWidth()/RATE, image.getHeight()/RATE);
image.setPosition(0f, 0f);
image.addListener(new DragListener() {
public void drag(InputEvent event, float x, float y, int pointer) {
Gdx.app.log("LOG", "DRAG IMAGE");
image.moveBy(x - image.getWidth() / 2, y - image.getHeight() / 2);
}
});
image.debug();
image.setBounds(image.getX(), image.getY(), image.getWidth(), image.getHeight());
stage = new Stage(new FitViewport(W, H));
stage.addActor(image);
Gdx.input.setInputProcessor(stage);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float delta = Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f);
stage.act(delta);
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
}
}
I need to have RATE bigger than 100f because Image actor has a Box2D Body attached. See here why.