简体   繁体   中英

InputProcessor/Multiplexer in libgdx

 public boolean touchDragged(int screenX, int screenY, int pointer) {

    if(check) {
        sprite.setPosition(screenX - sprite.getWidth() / 2, Gdx.graphics.getHeight() - screenY - sprite.getHeight() / 2);
        rect.setPosition(screenX - sprite.getWidth() / 2, Gdx.graphics.getHeight() - screenY - sprite.getHeight() / 2);
    }
    return false;
}

this is my method in the my custom input processor class i use Input multiplexer in my main because i have 2 classes . Simultaneous drag won't move the sprite i can only move one sprite at a time. My intention is to drag 2 sprites at the same time.

Thanks for your help and sorry for my bad English.

I don't know if this is the best approach, but a kinda-solution could be something like this:

1. Add a constant that let you know how many object you will allow to move at the same time :

private static final int MAX_TOUCHES = 4;

2. Add a collection with a fixed size , with this you'll manage all sprite that are currently being possible moving:

private final Array<Sprite> sprites = new Array<Sprite>(MAX_TOUCHES);

3. Now, in your class where you are handling touches, implement the touchDown() , touchDragged() and touchUp() :

/**
 * In the touchDown, add the sprite being touched
 **/
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    // Just allow 4 sprites to move at same time
    if(pointer >= MAX_TOUCHES) return true; 

    // Get the sprite at this current position...
    Sprite sprite = getSpriteAtThisPosition(screenX, screenY);

    // If sprite found, add to list with current pointer, else, do nothing
    if(sprite != null) {
        sprites.set(pointer, sprite);
    }
    return true;
}

getSpriteAtThisPosition() is just a method that return the first current sprite in that position, could return null .


/**
 * In the touchDragged, move this sprite
 **/
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    // Just allow 4 sprites to move at same time
    if(pointer >= MAX_TOUCHES) return false; 

    // Get the sprite with the current pointer
    Sprite sprite = sprites.get(pointer);

    // if sprite is null, do nothing
    if(sprite == null) return false;

    // else, move sprite to new position
    sprite.setPosition(screenX - sprite.getWidth() / 2, 
                       Gdx.graphics.getHeight() - screenY - 
                       sprite.getHeight() / 2);
    return false;
}

/**
 * In the touchUp, remove this sprite from the list
 **/
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    // Just allow 4 sprites to move at same time
    if(pointer >= MAX_TOUCHES) return true; 

    // remove sprite at pointer position
    sprites.set(pointer, null);
    return true;
}

The InputMultiplexer is not for dealing with two classes of object to move but to handle two input processors (or more) that you want to have - for example when you have more than one Stage and want to interact with player with each of them.

What you should do here would be to remember what pointer is attached to the touched object and then move it according to the pointer movement. The pointer is just id of for example finger enumerated from 0. So when you will touch screen with first finger it is 0 pointer, second one is 1 - BUT! If you will leave first finger keeping second the second is still 1 so it is perfect for this usage.

To handle remembering pointer id you also need to implement touchDown listener method

The code example would be like:

            HashMap<Integer, Sprite> ids = new HashMap<Integer, Sprite>();

            ...

            public boolean touchDown (int x, int y, int pointer, int button) 
            {
                Sprite sprite = getSprite(x, y);  //getSprite should iterate over all sprites in your game checking if x/y is inside one of them - you need to implement this one

                ids.put(pointer, sprite);

                return true;
            }

            public boolean touchUp (int x, int y, int pointer, int button) 
            {
                ids.remove(pointer); //removing from hashmap

                return false;
            }

            public boolean touchDragged (int x, int y, int pointer) 
            {
                Sprite sprite = ids.get(pointer);

                if(sprite != null)
                {
                    sprite.setPosition... //and so on
                }
                return false;
            }

For getting more information about multitouch handling you can check out this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM