简体   繁体   English

如何让LibGDX只检测一次点击/点击?

[英]How can I make LibGDX detect only a single tap/click?

I'm trying to do a basic counter, so every tap will increase a counter by one. 我正在尝试做一个基本的计数器,所以每次点击都会增加一个计数器。 I can get the counter to work but it's also increasing the count crazily when I hold down my finger/click. 我可以让计数器工作,但当我按住手指/点击时,它也会疯狂地增加计数。

Code: 码:

public void render() {
    boolean isTouched = Gdx.input.isTouched();

    if (isTouched) {
        System.out.println(Cash);
        Cash++;

    }

}

Also, while I'm here, how can you print an integer/float that will change every tap? 另外,虽然我在这里,你怎么能打印一个integer/float来改变每一个点击?

Like: font.draw(batch, Cash, 300, 260); 喜欢: font.draw(batch, Cash, 300, 260);

Straight up don't work. 直接不起作用。

What you are doing is polling the Input. 你正在做的是轮询输入。 But for what you want, an InputProcessor would be the way to go: 但是对于你想要的东西,一个InputProcessor将是你要走的路:

public class MyInputProcessor implements InputProcessor {
   @Override
   public boolean keyDown (int keycode) {
      return false;
   }

   @Override
   public boolean keyUp (int keycode) {
      cash++; //<----
      return false;
   }

   @Override
   public boolean keyTyped (char character) {
      return false;
   }

   @Override
   public boolean touchDown (int x, int y, int pointer, int button) {
      return false;
   }

   @Override
   public boolean touchUp (int x, int y, int pointer, int button) {
      return false;
   }

   @Override
   public boolean touchDragged (int x, int y, int pointer) {
      return false;
   }

   @Override
   public boolean touchMoved (int x, int y) {
      return false;
   }

   @Override
   public boolean scrolled (int amount) {
      return false;
   }
}

Set it in your create code: 在创建代码中设置它:

MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);

Reference: Libgdx Wiki Event-Handling 参考: Libgdx Wiki事件处理

how can you print an integer/float that will change every tap? 你怎么能打印一个整数/浮点数来改变每一个点击?
Like: font.draw(batch, Cash, 300, 260); 喜欢:font.draw(batch,Cash,300,260);
Straight up don't work. 直接不起作用。

BitmapFont#draw accepts a String, not an int/float. BitmapFont #draw接受String,而不是int / float。 You must use one of these: 你必须使用以下其中一个:

Integer.toString(Cash); //or
Float.toString(Cash);

Pro Tip: Don't start a variable name with Caps. 专业提示:不要使用Caps启动变量名称。 it should be cash . 它应该是cash

The InputProcessor is definitely the way to go longer term (event-based input is more robust, I think), but a built-in hack you can also use is the "justTouched" API : InputProcessor绝对是长期使用的方式(基于事件的输入更强大,我认为),但你也可以使用的内置hack是“justTouched”API

if (Gdx.input.justTouched()) {
    System.out.println(Cash);
    Cash++;
}

See https://code.google.com/p/libgdx/wiki/InputPolling 请参阅https://code.google.com/p/libgdx/wiki/InputPolling

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM