SoFunction
Updated on 2025-03-01

Detailed explanation Libgdx uses ShapeRenderer custom Actor in Android to solve the problem of not receiving Touch events

Detailed explanation Libgdx uses ShapeRenderer custom Actor in Android to solve the problem of not receiving Touch events

Today I achieved an effect in the project, mainly drawing a circle. For the convenience of subsequent use, encapsulate this circle in a custom Actor (CircleActot). When you want to display a circle in the future, just create a CircleActor. Some of the codes are as follows:

package ;

import ;

import ;
import ;
import .;
import .;
import ;
import .;
import ;
import ;

/**
 * Created by  on 17/4/17.
 *
 * A Widget currently used in the UnitMap, shown as a CIRCLE shape
 * if text not null, there would be a text drawn in the center of the circle
 */

public class CircleActor extends Actor {

  private float centerX;
  private float centerY;
  private String text;
  private float radius;

  private ShapeRenderer sr;
  private BitmapFont bitmapFont;

  public CircleActor(float x, float y, float radius) {
    this(x, y, radius, null);
  }

  public CircleActor(float x, float y, float radius, String text) {
     = x;
     = y;
     = radius;
     = text;

    sr = new ShapeRenderer();
  }

  @Override
  public void act(float delta) {
    (delta);
  }

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

    ();

    (());
    (());

    ();

    (centerX, centerY, radius);

    ();

    ();

    ...
  }

Then create a Stage object and add the CircleActor object to the Stage to display. But it is impossible to add a ClickLitener listener to this CircleActor object.

For example, the following code:

Stage stage = new Stage();

CircleActor ca = new CircleActor(100, 100, 50, "Hello World");
(new ClickListener(){
  public void click(){
    ("TAG", "ca is clicked");
  }
})

(ca);

The click method in the above code can never be called! After adjusting for more than half a day, I finally figured out the reason: Although the circle is drawn to a certain position on the screen in the draw method of CircleActor, there is actually not much connection between this ShapeRenderer and the Actor. The only connection is the following two code sentences, which means that the camera of ShapeRenderer and Actor objects are consistent.

(());
(());

However, at this time, the CircleActor does not set the true size and position. Therefore, to solve the above problem, it is necessary to make the size and position of the CircleActor consistent with the ShapeRenderer in the constructor!!

As shown in the following code, just add two lines of code:

public EfCircle(float x, float y, float radius, String text) {
     = x;
     = y;
     = radius;
     = text;

    //Solve the ShapeRenderer cannot get the Touch event    setPosition(centerX - radius, centerY - radius);
    setSize(radius * 2, radius * 2);

    sr = new ShapeRenderer();
  }

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!