How can I detect a click event on an Entity?
I use libgdx with Ashley.
Must I implement it by my self e.g. in touch down method of InputProcessor test if any entity is on this click position.
Or exist already a listener for Entity like the ClickListener for Actor in libgdx?

Thanks for your advice

  • \$\begingroup\$ What have you tried so far? I would probably have a clickable component which has the bounds of the entity and a system that listens for clicks/touches and loops through each clickable entity to see if any of the bounds intersect. \$\endgroup\$ – dfour Jun 28 '18 at 9:38
  • \$\begingroup\$ I haven't tried it. You do not have to show me any code either. My question is only Do I have to do it myself or is there already a listener? \$\endgroup\$ – Morchul Jun 28 '18 at 9:48
  • \$\begingroup\$ Ashley only comes with limited prebuilt systems which are meant to be extended from, more than directly used. So you will more than likely have to build you own system. \$\endgroup\$ – dfour Jun 28 '18 at 9:59
  • \$\begingroup\$ So it doesn't have any prebuild listeners. Thanks, now I will try it on my own. \$\endgroup\$ – Morchul Jun 28 '18 at 10:03

As far as I know Ashley does not have any pre-built systems that would handle click events and map them to entities.

A simple clickable system could easily be created using a clickable component and clickable system.

An example with an OrthographicCamera:

The clickable component with the rectangle bounds

public class ClickableComponent implements Component{
    public Rectangle bounds = new Rectangle(0,0,10,10);

A Transform component to store location

public class TransformComponent implements Component{
    public final Vector3 position = new Vector3();

With a Clickable System

public class ClickableSystem extends IteratingSystem{

    private ComponentMapper<ClickableComponent> cc;
    private ComponentMapper<TransformComponent> tc;
    private OrthographicCamera camera;
    public ClickableSystem(OrthographicCamera cam) {
        cc = ComponentMapper.getFor(ClickableComponent.class);
        tc = ComponentMapper.getFor(TransformComponent.class);
        camera = cam;
    protected void processEntity(Entity entity, float deltaTime) {
        ClickableComponent click = cc.get(entity);
        TransformComponent transform = tc.get(entity);
        //update click bounds
        click.bounds.x = transform.position.x;
        click.bounds.y = transform.position.y;
            // touching
            Vector3 clickPosition = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(),0));
            if(click.bounds.contains(clickPosition.x, clickPosition.y)){
                //object clicked
                //do your thing
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.