【问题标题】:LibGDX Actor in stage not receiving click events舞台中的 LibGDX Actor 未接收到点击事件
【发布时间】:2019-05-11 11:24:27
【问题描述】:

编辑:

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

OG 帖子:

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

没有响应 - 当我尝试点击演员时。 假设可以改变颜色、提高速度并跟踪输入 x/ys。

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

演员由 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();
   }

【问题讨论】:

  • 将您的代码上传到某个地方,或者最好告诉我自我们上一个工作版本以来您所做的更改。有一个很好的调试方法。您只需返回该工作版本并仔细添加新行并检查它是否仍在工作
  • 您好。我还没有学习如何正确使用 git(另外,无论如何都在手机上工作/没有电脑),所以我只需将这两个文件添加到我的 pastebin 并希望你不介意完成制作新文件的过程libgdx 项目... x_x 粘贴:pastebin.com/u/StudioGilliam/1/0/1/?guest=1

标签: java android input libgdx


【解决方案1】:

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();

这是不合逻辑的。

【讨论】:

  • @StudioGilliam 你到底不明白什么。我可以解释。您必须更仔细地研究问题并进行调试,而不是将遇到的每个问题都发布到 StackOverflow
  • @StudioGilliam 我不能每次都为你这样做
猜你喜欢
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多