处于阶段的LibGDX Actor未收到点击事件

时间:2019-05-11 11:24:28

标签: java android input libgdx

编辑:

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

OG帖子:

  1. 子类演员。
  2. 将演员放到舞台上。
  3. 在Actor.constructor中添加了一个ClickListener。
  4. 将舞台设置为inputProcessor。
  5. 但是当我单击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();
   }

Ball.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 个答案:

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

这是不合逻辑的。