2
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\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
    Commented Jun 28, 2018 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
    Commented Jun 28, 2018 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
    Commented Jun 28, 2018 at 9:59
  • \$\begingroup\$ So it doesn't have any prebuild listeners. Thanks, now I will try it on my own. \$\endgroup\$
    – Morchul
    Commented Jun 28, 2018 at 10:03

1 Answer 1

3
\$\begingroup\$

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;
    
    
    @SuppressWarnings("unchecked")
    public ClickableSystem(OrthographicCamera cam) {
        super(Family.all(ClickableComponent.class,TransformComponent.class).get());
        cc = ComponentMapper.getFor(ClickableComponent.class);
        tc = ComponentMapper.getFor(TransformComponent.class);
        camera = cam;
    }
    
    @Override
    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;
        
        if(Gdx.input.isButtonPressed(Buttons.LEFT)){
            // touching
            Vector3 clickPosition = camera.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(),0));
            System.out.println(clickPosition+":"+click.bounds.toString());
            if(click.bounds.contains(clickPosition.x, clickPosition.y)){
                //object clicked
                //do your thing
                System.out.println("Touched"+entity.toString());
            }
        }
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .