我在屏幕的构造函数中设置stage作为输入处理器,
public GameScreen() {
stage = new GameStage();
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
在舞台上,我正在调用这样的事件处理程序,
private OrthographicCamera camera;
private Box2DDebugRenderer renderer;
private Vector3 touchPoint;
private static final int VIEWPORT_WIDTH = 1280;
private static final int VIEWPORT_HEIGHT = 720;
public GameStage() {
touchPoint = new Vector3();
setupCamera();
this.setViewport(new ScreenViewport(camera));
renderer = new Box2DDebugRenderer();
setUpWorld();
}
private void setUpWorld() {
world = WorldUtils.createWorld();
world.setContactListener(this);
setUpGround();
}
private void setUpGround() {
addActor(new Rock(WorldUtils.createGroundRock(world, 320 / Constants.PIXELS_TO_METERS, Constants.GROUND_HEIGHT, 640f / Constants.PIXELS_TO_METERS, 130f / Constants.PIXELS_TO_METERS)));
}
private void setupCamera() {
camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
}
@Override
public void act(float delta) {
super.act(delta);
accumulator += delta;
while (accumulator >= delta) {
world.step(TIME_STEP, 6, 2);
accumulator -= TIME_STEP;
}
camera.update();
}
@Override
public void draw() {
super.draw();
renderer.render(world, camera.combined);
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
translateScreenToWorldCoordinates(screenX, screenY);
Gdx.app.log("LOG", "Touch Coords " + screenX + " : " + screenY);
return super.touchDown(screenX, screenY, pointer, button);
}
private void translateScreenToWorldCoordinates(int x, int y) {
getCamera().unproject(touchPoint.set(x, y, 0));
}
@Override
public void beginContact(Contact contact) {
//TODO
}
@Override
public void endContact(Contact contact) {
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
}
@Override
public boolean scrolled(int amount) {
camera.zoom -= .42 * amount;
return true;
}
现在我有一个actor,它的构造函数中有一个事件处理程序,
public class Rock extends Actor {
Texture texture = new Texture(Gdx.files.internal("brick.png"));
Sprite sprite = new Sprite(texture, 150, 130);
public Rock(Body body) {
setBounds(body.getPosition().x * Constants.PIXELS_TO_METERS, body.getPosition().y * Constants.PIXELS_TO_METERS, 150, 130);
setTouchable(Touchable.enabled);
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("LOG", "TAPPED");
return super.touchDown(event, x, y, pointer, button);
}
});
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(sprite, getX(), getY(), getWidth(), getHeight());
}
@Override
protected void positionChanged() {
sprite.setPosition(getX(), getY());
super.positionChanged();
}
}
现在,舞台中的主输入处理程序正常工作,但不是actor中的主输入处理程序。即使我删除了阶段中的输入处理程序,它也不起作用。我猜它需要以不同的方式实现,但不确定。如何确保这些输入处理程序都能正常工作。另外,如果我有几个Rock演员,我需要输入处理程序才能使所有这些演员工作。
答案 0 :(得分:0)
我建议你使用Singleton设计模式来解决这个问题。
<强> Theoratically:强>
<强>实际上:强>
创建一个扩展 InputProcessor 的类,我们称之为 UiInput :
public class UiInput implements InputProcessor {
@Override
public boolean keyDown(int keycode) {
//not a great way but what ever.
if (keycode == Keys.ESCAPE) {
GameConstants.gameScreen.pauseOrResume = !GameConstants.gameScreen.pauseOrResume;
if (!GameConstants.gameScreen.pauseOrResume) {
GameConstants.gameScreen.resume();
} else {
GameConstants.gameScreen.pause();
}
return true;
} else {
return false;
}
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {return false;}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {return false;}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {return false;}
@Override
public boolean mouseMoved(int screenX, int screenY) {return false;}
@Override
public boolean scrolled(int amount) {return false;}
}
您可以创建多个类。
接下来,您将为所有InputProcessors创建一个实例,并包括InputMultiplexer
/* UI-Input */
private static UiInput uiInput = new UiInput();
/* DebugInput */
private static DebugInput debugInput = new DebugInput(); //etc.
/* inputMultiplexer */
public static InputMultiplexer inputMultiplexer = new InputMultiplexer(uiInput, debugInput);
现在,不要将舞台设置为InputProcessor - 在GameScreen中,您可以将InputMultiplexer作为InputProcessor:
public GameScreen() {
stage = new GameStage();
//Gdx.input.setInputProcessor(stage);
Gdx.input.setInputProcessor(inputMultiplexer);
}