Detect when touch is dragged over a button

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Detect when touch is dragged over a button

Postby DRing » Sat Feb 24, 2018 8:14 pm

I want to make some on screen buttons (that part is easy) but what Im wanting to know how to do is detect when a touch event (even if it isnt started on the button) is dragged over the button. Right now Im using a HUD, which has its own stage and viewport for the controls. the stage is the inputprocessor

Code: Select all
public Hud(GameScreen screen){
        this.basePlayer = screen.basePlayer;
        score = 0;
        cam = new OrthographicCamera();
        viewport = new FitViewport(Constants.V_WIDTH,Constants.V_HEIGHT, cam);
        stage = new Stage(viewport, screen.getGame().batch);

        Gdx.input.setInputProcessor(stage);

        Table table = new Table();
        table.top();
        table.setFillParent(true);

        timeLabel = new Label(String.format("%03d", time), new LabelStyle(new BitmapFont(), Color.WHITE));
        table.row();
        table.add(timeLabel).expandX();

        Table buttonTable = new Table();
        buttonTable.setFillParent(true);
        buttonTable.right().bottom();

        Image jumpImg = new Image(screen.getGame().assetManager.manager.get("flatDark25.png", Texture.class));
        jumpImg.setSize(75, 75);
        jumpImg.addListener(new InputListener() {

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                basePlayer.jump();
                return true;
            }

        });

        buttonTable.add(jumpImg).size(jumpImg.getWidth(), jumpImg.getHeight());

        stage.addActor(table);
        stage.addActor(buttonTable);
    }


So trying to detect when any touch on the screen gets dragged over the button
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Re: Detect when touch is dragged over a button

Postby evilentity » Sat Feb 24, 2018 9:20 pm

You should check what sort of stuff is available in the InputListener
Code: Select all
jumpImg.addListener(new InputListener(){
    @Override public void enter (InputEvent event, float x, float y, int pointer, Actor fromActor) {
        Gdx.app.log("Welp", "enter");
    }

    @Override public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
        Gdx.app.log("Welp", "exit");
    }
});
Looking for a freelancer? PM me!
evilentity
 
Posts: 4250
Joined: Wed Aug 24, 2011 11:37 am

Re: Detect when touch is dragged over a button

Postby DRing » Sat Feb 24, 2018 9:49 pm

That is exactly what Im looking for but tried this:

Code: Select all
public Hud(GameScreen screen) {
        this.basePlayer = screen.basePlayer;
        score = 0;
        cam = new OrthographicCamera();
        viewport = new FitViewport(Constants.V_WIDTH,Constants.V_HEIGHT, cam);
        stage = new Stage(viewport, screen.getGame().batch);

        Gdx.input.setInputProcessor(stage);

        Table table = new Table();
        table.top();
        table.setFillParent(true);

        timeLabel = new Label(String.format("%03d", time), new LabelStyle(new BitmapFont(), Color.WHITE));
        table.row();
        table.add(timeLabel).expandX();

        Table buttonTable = new Table();
        buttonTable.setFillParent(true);
        buttonTable.right().bottom();

        jumpImg = new Image(screen.getGame().assetManager.manager.get("flatDark25.png", Texture.class));
        jumpImg.setSize(75, 75);
        jumpImg.addListener(new InputListener() {

            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                Gdx.app.log("Enter", "Jump Entered");
                basePlayer.jump();
            }

            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                basePlayer.jump();
                return true;
            }

        });

buttonTable.add(jumpImg).size(jumpImg.getWidth(), jumpImg.getHeight());

        stage.addActor(table);
        stage.addActor(buttonTable);
    }


And dont get the log output of the jump. But clicking it does work
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Re: Detect when touch is dragged over a button

Postby DRing » Sat Feb 24, 2018 10:05 pm

Figured out I needed to call stage.act() and didnt have that in the update method
DRing
 
Posts: 104
Joined: Thu Oct 20, 2011 7:42 pm

Re: Detect when touch is dragged over a button

Postby maosk21 » Mon Mar 26, 2018 8:16 pm

In my games, i allways create a rectangle with the size 1x1, or 2x2 pixels to store player touch cordinates in.

And my buttons are also allways a rectangle like 20x20 or something.

Then just:

Code: Select all

if(gdx.input.isTouched(){

  if(touch.overlaps(button1){
     do stuff
  }
}



Easiest way i think.
maosk21
 
Posts: 7
Joined: Sun Jun 11, 2017 1:24 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest