【问题标题】:Libgdx: pause without triggering justtouchLibgdx:暂停而不触发justtouch
【发布时间】:2017-04-16 09:06:46
【问题描述】:

我最近问this question,它运行良好,我唯一遇到的问题是整个游戏是基于触摸事件的,当用户触摸屏幕时,会创建一个对象。

现在发生的情况是,当用户触摸暂停按钮(纹理打包器)时,会创建一个对象并暂停游戏。如果触摸暂停,我想防止创建对象。我曾经能够做这样的事情:

private Vector3 touchPos; 
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); 
camera.unproject(touchPos); 

if (Gdx.input.justTouched()) {
 if (touchPos.x > pauseX && touchPos.x < pauseX + pauseX) {
  if (touchPos.y > pauseY && touchPos.y < pauseX + pauseY) {
   setGamePause(!getGamePause());
  }}}

但似乎没有使用纹理打包器,也许我的实现是错误的,不确定,还有其他方法吗?

private float pauseY = Gdx.graphics.getHeight() - 115;
private float pauseX = Gdx.graphics.getWidth() / 6;
button.setSize(150, 150)

它在屏幕的左上角

【问题讨论】:

  • 不确定纹理打包器与此有什么关系?
  • @1blustone 所以是我以错误的方式实现上述方法吗?或者有其他方法吗
  • 我的意思是我不太清楚你的标题是什么意思 - 所以你想要做的就是在按下按钮时停止/恢复一切(包括对象创建)?看看Ashley 并为一切创建系统,当游戏为真时停止调用engine.update。当然,请将暂停按钮放在 Engine 之外,这样您就可以恢复它了 :)
  • @1blustone 是的,我不想触发只是触摸或像你说的那样

标签: java libgdx


【解决方案1】:

如果pauseXpauseY 分别是矩形/按钮的左侧和右侧,那么

您需要按钮/矩形区域的宽度和高度,例如假设buttonWidthbuttonHeight

if(Gdx.input.justTouched()) {
   if (touchPos.x > pauseX && touchPos.x < pauseX + button.getWidth()) {
      if (touchPos.y > pauseY && touchPos.y < pauseY + button.getHeight()) {
          setGamePause(!getGamePause());
      }
   }
}

请检查测试:

public class GdxTest extends Game  implements  InputProcessor{

    private Stage stage;
    Vector3 vector3;
    TextButton button;
    float pauseX,pauseY;

    @Override
    public void create() {

        vector3=new Vector3();
        ExtendViewport extendViewport=new ExtendViewport(700,1200,new OrthographicCamera());
        stage=new Stage(extendViewport);

        Skin skin=new Skin(Gdx.files.internal("skin/uiskin.json"));
        skin.get("font-label", BitmapFont.class).getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

        pauseX=300;
        pauseY=500;
        button=new TextButton("Pause",skin);
        button.setPosition(pauseX,pauseY);

        stage.addActor(button);
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render() {
        super.render();

        Gdx.gl.glClearColor(0,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();
        stage.act();

        ////Touch Detection without processor

        vector3.set(Gdx.input.getX(),Gdx.input.getY(),0);
        stage.getCamera().unproject(vector3);

        if(Gdx.input.justTouched()){
             if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth()  && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
                 System.out.println("TOuched");
         }

         ///////
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width,height);
        stage.getViewport().update(width,height);
    }

    @Override
    public void dispose() {
       stage.dispose();
    }

    @Override
    public boolean keyDown(int keycode) {
        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 button1) {

        vector3.set(screenX,screenY,0);
        stage.getCamera().unproject(vector3);

        if(vector3.x>pauseX && vector3.x<pauseX+button.getWidth() && vector3.y>pauseY && vector3.y<pauseY+button.getHeight())
            System.out.println("TOuched");


        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;
    }
}

【讨论】:

  • 没用,请检查我更新的问题,也许是我的错
  • 我在答案中添加了一个测试类。
  • 今晚会测试并告诉你,我测试了第一部分没有工作,离开并忘记测试public boolean touchDown
  • TestClass 的第一部分?
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多