Jump to content
  • Advertisement
Sign in to follow this  
menyo

Understanding the input processor.

This topic is 1438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am somewhat lost as to how the LibGDX input processor works and how to implement it for what i want. I simple want to detect if a touch was made or released in a specific class. Something as simple as "if (touchUp) { //do stuff }".

 

But the input processor works differently, it just runs the code there when a touch is released. The only way i can think of doing what i want feels hacky: create globals in my class and set these to true from the InputProcessor and at the end of the update cycle in my class I put them false again.

//MyInputProcessor

@Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        MyClass.touched = true;
        return false;
    }


    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        MyClass.released = true;
        return false;
    }


//MyClass


public static touched = false;
public static released = false;


@Override
public void render(float delta) {
if (touched)
  //do stuff
if (released)
  //do stuff


//end of update
touched = false;
released = false;
}

It just doesn't feel right to do it this way. If i want to do all the touchup/touchdown code inside the InputProcessor class i have to make tons of fields in MyClass global. Am i missing something? I just need something like "if (getInputProcessor().getTouchDown) { //Do stuff }".

 

Also, if i want to have multiple pointers for fingers (Which is highly unlikely) the above example does not work.

Edited by menyo

Share this post


Link to post
Share on other sites
Advertisement

I don't have experience with Libgdx, but looking at the documentation I see InputProcessor is an interface, so I guess the intended use is to implement that interface directly in the class that needs input information. Instead of making MyInputProcessor and MyClass, just make MyClass be an InputProcessor too.

 

The documentation also says the methods from the interface will be called before the call to render, so if you need the touch information for the render part you'll need some variables to hold the information. It will be similar to what you wrote, but don't need to be globals or statics, they could be a private attribute on the class.

 

Also, I'm not sure you need "touched" and "released" booleans, use just "touched" and set it to false on touchUp event. Maybe you want to also record the state of the "touched" variable in the previous iteration, so you can detect the exact moment where it was touched/released instead of just knowing the current state.

Share this post


Link to post
Share on other sites

I cover this in my LibGDX tutorial series although it was actually in the keyboard example I did it.

 

 

Basically, if I am reading you correctly, you want to poll input (on demand) as opposed to event driven?

 

Good news is, you can do this.  Yeah you essentially would have to track all these values ( position, finger, etc ), but fortunately LibGDX already does exactly that for you.  Take a look at the Input class

 

To get touch position on of first finger ( remember, multi touch, multiple pointers ), you simply do:

 

Gdx.input.getX(0); // 0 is 1st finger.  1 for second, 2 for third, etc

 

This returns the X coordinate of the current touch if applicable.  Check isTouched or isTouch(n) before calling getX/Y.

 

Using the input class you can easily poll input.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!