java - 阶段中的 LibGDX Actor 未收到点击事件

标签 java android input libgdx

编辑:

我认为这与我 setBounds() 的方式有关... 我有一个类似的项目(实际上有效),涉及一个移动的盒子,点击时会改变颜色......一个盒子 - 而不是一个圆圈。我认为这是一个线索。

原创帖子:

  1. 子类 Actor。
  2. 让 Actor 进入舞台。
  3. 在 Actor.constructor 中添加了一个 ClickListener。
  4. 将 Stage 设置为 inputProcessor。
  5. 但是当我点击 Actor 时,似乎什么也没有发生。

没有响应 - 当我尝试单击 Actor 时。 它假设改变颜色、提高速度和跟踪输入 x/ys。

这很奇怪,因为我构建了一个非常相似的有效程序。那为什么不呢?

Actor 由 ShapeRenderer 绘制的圆圈表示。我假设它的“点击”边界一定只是一个矩形?

HelloGDXGame.java

@Override
   public void create() {
      SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
      SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
      rowHeight = 80;
      touchPos = new Vector3();

      batch = new SpriteBatch();

      font = new BitmapFont();
      font.setColor(Color.PINK);
      font.setScale(4.0f);

    ball = new Ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 180, 90, this);

      stage = new Stage(new StretchViewport(SCREEN_WIDTH, SCREEN_HEIGHT));

      stage.addActor(ball);

      Gdx.input.setInputProcessor(stage);
   }

   @Override
   public void render()
   {        
      Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      // Translate inputs x/y
      touchPos.x = Gdx.input.getX();
      touchPos.y = (Gdx.input.getY() - Gdx.graphics.getHeight()) * -1;

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

      // UI, to check it all works (it doesn't)
      batch.begin();
      font.draw(batch, "Ball Class: Taps: " + ball.getTaps(), 64, SCREEN_HEIGHT - (rowHeight * 8));
      font.draw(batch, "Main Class: Touch X: " + touchPos.x, 64, SCREEN_HEIGHT - (rowHeight * 7));
      font.draw(batch, "Main Class: Touch Y: " + touchPos.y, 64, SCREEN_HEIGHT - (rowHeight * 6));
      font.draw(batch, "Ball Class: Touch X: " + ball.getScreenX(), 64, SCREEN_HEIGHT - (rowHeight * 5));
      font.draw(batch, "Ball Class: Touch Y: " + ball.getScreenY(), 64, SCREEN_HEIGHT - (rowHeight * 4));
      batch.end();
   }

球.java

public Ball(float xPos, float yPos, float xSpeed, float ySpeed, HelloGdxGame game) {
      super();
      this.game = game;
      renderer = new ShapeRenderer();

      taps = 0;
      radius = 196;

      super.setBounds(xPos - radius, yPos - radius, radius * 2, radius * 2);

// just to check it's working
screenXY = new Vector3(0, 0, 0);

      this.xSpeed = xSpeed;
      this.ySpeed = ySpeed;

    this.setTouchable(Touchable.enabled);

      this.addListener(new ClickListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                doStuff(x, y);
                return false;
            }
         });

      changeColour();  // sets random color
   }

   private void doStuff(float screenX, float screenY) {
      screenXY.x = screenX;
      screenXY.y = screenY;
      changeColour();
      taps++;

      // Increase speed
      if (xSpeed > 0) xSpeed++;
      else xSpeed--;
      if (ySpeed > 0) ySpeed++;
      else ySpeed--;
   }

   @Override
   public void act(float delta) {
      super.act(delta);

// move
      super.setX(super.getX() + (xSpeed * delta));
    super.setY(super.getY() + (ySpeed * delta));

// Bounce horizontally
      if (super.getX() < 0 || super.getX() + (radius / 2) > Gdx.graphics.getWidth()) {
         super.setX(super.getX() - (xSpeed * delta));
        xSpeed *= -1;
      }

// Bounce vertically
      if (super.getY() < 0 || super.getY() + (radius / 2) > Gdx.graphics.getHeight()) {
         super.setY(super.getY() - (ySpeed * delta));
         ySpeed *= -1;
      }
   }

   @Override
   public void draw(Batch batch, float parentAlpha) {
      super.draw(batch, parentAlpha);

    renderer.begin(ShapeRenderer.ShapeType.Filled);
      renderer.setColor(r, g, b, 1);
      renderer.circle(super.getX(), super.getY(), radius);
      renderer.end();
   }

最佳答案

1) 从here中取出一个改变的球

2) 改变

SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;

SCREEN_WIDTH = Gdx.graphics.getWidth();
SCREEN_HEIGHT = Gdx.graphics.getHeight();

这是不合逻辑的。

关于java - 阶段中的 LibGDX Actor 未收到点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089869/

相关文章:

java - 哪个版本的 Eclipse 与 JDK 1.5 兼容?

android - 等待 Crashlytics - 在 Android Eclipse 中

android - 应用 dexguard 后 onClick() 不起作用。有什么办法可以解决这个问题吗?

java - 设置项目单击监听器事件没有受到影响吗?

java - 编写一个程序,允许用户输入一个字符串,然后打印该字符串中的字母,并用逗号分隔

Android 崩溃,java.lang.IllegalArgumentException : Window type can not be changed after the window is added

javascript - 在 div 中仅添加某些输入

java - 输入错误。即使在接受新输入后仍然使用旧输入

ios - css 输入数字字段在某些版本的 ios 上呈现奇怪

java - 在 Java 中处理未初始化的字符串