Libgdx:暂停而不触发justtouch

时间:2017-04-16 09:06:47

标签: java libgdx

我最近问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)

它位于屏幕的左上角

1 个答案:

答案 0 :(得分: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;
    }
}