【发布时间】:2016-06-06 05:40:49
【问题描述】:
我有一个设置了拖动监听器的 Actor:
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);
}
});
这个演员被添加到舞台:
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);
现在,如果我使用米像素比 1:1,拖动效果很好,但如果我使用其他比例,比如 1:160,拖动事件不会触发。
在调整大小的方法中,我更新了相机:
camera.viewportHeight = H/RATE;
camera.viewportWidth = W/RATE;
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
我读到如果我使用舞台,我不必从屏幕坐标转换为相机坐标。演员已经设置了位置、宽度、高度和边界。我做错了什么?
更新:
代码如下:
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();
}
}
我需要 RATE 大于 100f,因为 Image actor 附加了 Box2D Body。见here为什么。
【问题讨论】:
标签: libgdx scale viewport drag orthographic