我有一个屏幕类,它将输入处理器设置为阶段类Gdx.input.setInputProcessor(gameStage);
,我的舞台类实现了inputProcessor
public class GameStage extends Stage implements InputProcessor
。我的阶段类中的输入方法没有调用,我无法弄清楚原因。我用这种方法测试了它
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
batch.begin();
font.draw(batch, "text", 200, 50);
batch.end();
return super.touchDown(screenX, screenY, pointer, button);
}
但是没有绘制文本,当我在act方法中使用Gdx.input.isTouched()
进行尝试时,它确实有效。
由于
编辑:所有代码
public class GameScreen implements Screen {
private Game game;
private GameStage gameStage;
private OrthographicCamera camera;
private Viewport viewport;
private Box2DDebugRenderer renderer;
public GameScreen(Game game) {
this.game = game;
this.camera = new OrthographicCamera();
this.viewport = new ScreenViewport(camera);
this.gameStage = new GameStage(viewport);
this.renderer = new Box2DDebugRenderer();
Gdx.input.setInputProcessor(gameStage);
}
public OrthographicCamera getCamera() {
return this.camera;
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(247/255.0f, 247/255.0f, 247/255.0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.act();
renderer.render(gameStage.getWorld(), camera.combined);
gameStage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
舞台课程:
public class GameStage extends Stage {
private World world;
private Player player;
private BitmapFont font;
private Box2DDebugRenderer renderer;
public GameStage(Viewport viewport) {
super(viewport);
font = new BitmapFont();
setUpWorld();
}
public void setUpWorld() {
world = WorldUtils.createWorld();
player = new Player(WorldUtils.createPlayer(world));
}
@Override
public void draw() {
super.draw();
getBatch().begin();
font.draw(getBatch(), "Position: X=" + player.getX() + " Y=" + player.getY(), 200, 50);
getBatch().end();
}
@Override
public void act(float delta) {
super.act(delta);
world.step(1 / 300f, 6, 2);
}
public World getWorld() {
return this.world;
}
public Player getPlayer() {
return this.player;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
player.move();
super.touchDown(screenX, screenY, pointer, button);
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return super.touchDragged(screenX, screenY, pointer);
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return super.touchUp(screenX, screenY, pointer, button);
}
}
玩家类:
public class Player extends Actor{
private Body body;
public Player(Body body) {
this.body = body;
}
public void move() {
body.applyForceToCenter(50000, 200, true);
}
}
答案 0 :(得分:1)
不要在输入处理程序中绘制。只需存储坐标。它不起作用,因为你试图在游戏循环之外画画。同样返回false而不是父方法,当您返回true时,输入处理器会调用所有其他匹配的输入处理程序。这样的事情应该做到。
boolean touched;
void draw(Batch batch)
if(touched){
batch.begin();
font.draw(batch, "text", 200, 50);
batch.end();
}
}
@Override
public boolean touchDown(int screenX, int screenY, int
pointer , int button) {
super.touchDown(screenX, screenY, pointer, button);
touched = true;
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int
pointer , int button) {
super.touchUp(screenX, screenY, pointer, button);
touched = false;
return false;
}
编辑:同时删除implements InputProcessor
舞台类已经实现了该类,因此您不必这样做。这可能是导致处理程序被忽略的原因。您将父类的输入处理器设置为主处理程序,但是您将覆盖接口的输入处理程序而不是父级,因此它们不会被看到。
编辑2:尝试实现InputProcessor而不是扩展Stage。
public class GameStage InputProcessor {
private Stage stage;
private InputMultiplexer input;
private World world;
private Player player;
private BitmapFont font;
private Box2DDebugRenderer renderer;
public GameStage(Viewport viewport) {
stage = new Stage(viewport);
font = new BitmapFont();
setUpWorld();
input = new InputMultiplexer();
input.addProcessor(this);
input.addProcessor(stage);
}
public void setUpWorld() {
world = WorldUtils.createWorld();
player = new Player(WorldUtils.createPlayer(world));
stage.addActor(player)
}
@Override
public void draw() {
stage.draw();
getBatch().begin();
font.draw(getBatch(), "Position: X=" + player.getX() + " Y=" + player.getY(), 200, 50);
getBatch().end();
}
@Override
public void act(float delta) {
stage.act(delta);
world.step(1 / 300f, 6, 2);
}
public World getWorld() {
return this.world;
}
public Player getPlayer() {
return this.player;
}
public InputAdapter getInput(){
return this.input;
}
public Stage getStage(){
return this stage;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
player.move();
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return true;
}
}
答案 1 :(得分:0)
如果您想让舞台响应用户输入以及自己回复用户输入,您需要使用输入multiplexer。
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(myStage); // allows stage actors to respond to user input
multiplexer.addProcessor(new MyGameInputProcessor()); // allows you to repond to user actions
Gdx.input.setInputProcessor(multiplexer);
答案 2 :(得分:0)
希望您以这种方式做事:
public class GdxTest extends Game {
MyStage myStage;
@Override
public void create() {
myStage =new MyStage();
Gdx.input.setInputProcessor(myStage);
}
@Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myStage.draw();
myStage.act();
}
}
MyStage
类
public class MyStage extends Stage {
BitmapFont font;
public MyStage(){
font=new BitmapFont();
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Gdx.app.log("MyStage","Inside touchDown method");
drawText();
return super.touchDown(screenX, screenY, pointer, button);
}
@Override
public void act() {
super.act();
//if(Gdx.input.isTouched()) {
// drawText();
//}
}
public void drawText(){
getBatch().begin();
font.draw(getBatch(), "text", 200, 50);
getBatch().end();
}
}
根据您的说法:当您从drawText()
方法调用touchDown(..)
时,文字无法绘制,但如果您是通过act()
方法调用它,那么您就是&#39 ;重新获取屏幕上的文字。
文字未呈现的原因:
每当您触摸从drawText()
方法调用的屏幕touchDown(..)
方法时,该方法将在下一个render()
调用时清除,以便您在屏幕上注明,但另一方面如果您调用{{ 1}}来自drawText()
act()
render()
方法的ApplicationListener
方法,因此在清除帧缓冲区并设置颜色缓冲区后进行文本绘制。
解决方案:
您需要在每个渲染调用上调用drawText()
,以覆盖draw()
的{{1}}方法并使用标记调用MyStage
。
drawText()
并更改boolean drawText;
@Override
public void draw() {
super.draw();
if(drawText) {
drawText();
}
}
方法
touchDown(..)
两个建议:
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
drawText=true;
return super.touchDown(screenX, screenY, pointer, button);
}
类已经通过他的适配器类实现Stage
,因此需要再次实现。
InputProcessor
类拥有自己的私有批量引用,因此无需创建自己的,您可以使用Stage
方法。