简体   繁体   English

libgdx 中的输入处理器/多路复用器

[英]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 .这是我在我的自定义输入处理器类中的方法我在我的主要使用输入多路复用器,因为我有 2 个类。 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.我的目的是同时拖动 2 个精灵。

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 : 1. 添加一个常量,让您知道允许同时移动多少个对象

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: 2. 添加一个固定大小的集合,这样你就可以管理当前可以移动的所有精灵:

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() : 3.现在,在您处理触摸的类中,实现touchDown()touchDragged()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 . getSpriteAtThisPosition()只是一种返回该位置的第一个当前精灵的方法,可以返回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. InputMultiplexer不是用于处理要移动的两类对象,而是用于处理您想要拥有的两个(或更多)输入处理器 - 例如,当您有多个Stage并且想要与玩家进行交互时。

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!指针只是例如从 0 枚举的手指的 id。所以当你用第一根手指触摸屏幕时,它是0指针,第二个是1 - 但是! If you will leave first finger keeping second the second is still 1 so it is perfect for this usage.如果你让第一根手指保持第二,第二根仍然是1,所以它非常适合这种用法。

To handle remembering pointer id you also need to implement touchDown listener method要处理记住指针 id,您还需要实现touchDown侦听器方法

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 .有关多点触控处理的更多信息,您可以查看本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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