【问题标题】:InputProcessor/Multiplexer in libgdxlibgdx 中的输入处理器/多路复用器
【发布时间】:2015-10-07 15:01:40
【问题描述】:
 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;
}

这是我在自定义输入处理器类中的方法,我在主程序中使用输入多路复用器,因为我有 2 个类。同时拖动不会移动精灵,我一次只能移动一个精灵。 我的意图是同时拖动 2 个精灵。

感谢您的帮助,抱歉我的英语不好。

【问题讨论】:

  • 您想一次移动多个精灵吗?
  • 是的,这是我的意图
  • 谢谢! @大卫安德顿
  • 欢迎 :) 不幸的是我不知道任何 java - 但我相信很快有人会提供帮助
  • 这就是pointer的原因,libgdx允许你在屏幕上有20种不同的触摸

标签: java libgdx


【解决方案1】:

我不知道这是否是最好的方法,但一种解决方案可能是这样的:

1.添加一个常量,让您知道您将允许同时移动多少个对象

private static final int MAX_TOUCHES = 4;

2。添加一个固定大小的集合,这样您就可以管理当前可能移动的所有精灵:

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

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() 只是一个返回该位置的第一个当前精灵的方法,可以返回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;
}

【讨论】:

  • 谢谢我一回家就试试这个
【解决方案2】:

InputMultiplexer 不是用于处理要移动的两类对象,而是用于处理您想要拥有的两个(或更多)输入处理器 - 例如,当您有多个 Stage 并希望与之交互时玩家与他们每个人。

你应该做的是记住哪个指针附加到被触摸的对象,然后根据指针的移动来移动它。指针只是从 0 枚举的例如手指的 id。因此,当您用第一根手指触摸屏幕时,它是 0 指针,第二个是 1 - 但是!如果您将第一根手指保持在第二根,那么第二根手指仍然是 1,因此非常适合这种用法。

要处理记住指针 id,您还需要实现 touchDown 侦听器方法

代码示例如下:

            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;
            }

有关多点触控处理的更多信息,您可以查看this tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2015-09-03
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多