【问题标题】:handling multitouch events in libgdx (on android)在 libgdx 中处理多点触控事件(在 android 上)
【发布时间】:2014-08-02 03:36:22
【问题描述】:

嘿,所以我正在使用 libgdx 开发游戏,并且我正在使用输入多路复用器来处理基于触摸和键盘的输入的输入,这是我当前正在实现的具有相关 @Overide 方法的类。 (我开始只处理键盘事件以便于快速开发,现在已经重新实现以处理触摸事件)无论如何我已经按照我希望的方式映射了所有内容,但是在触摸设备上我似乎无法正确实现多点触控,即。当我向左或向右移动时,我也无法跳跃或投掷星星我没有在任何地方看到使用这些设备的相同帐户。 (在桌面上我可以运行+ jum+ throwstar 等。)

无论如何,这里是多路复用器/ InputProccesor 中最相关的代码。如果我遗漏了任何相关信息,请不要犹豫,我可以更新它。

public class GameScreen implements Screen, GestureListener, InputProcessor{

    GestureDetector gestureDetector ;
    String message;
    int directionPointer, actionPointer;
  public GameScreen(Game game, int levelNum){

    controller = new BobController(world, this);
    InputMultiplexer im = new InputMultiplexer();
    GestureDetector gd = new GestureDetector(this);
    im.addProcessor(gd);
    im.addProcessor(this);

    Gdx.input.setInputProcessor(im);
  }

  @Override
  public boolean keyDown(int keycode) {
    if (keycode == Keys.LEFT)
        controller.leftPressed();
    if (keycode == Keys.RIGHT)
        controller.rightPressed();
    if (keycode == Keys.Z)
        controller.jumpPressed();
    if (keycode == Keys.C)
        controller.throwPressed();
    if (keycode == Keys.X)
        controller.punchPressed();
    return true;
}

@Override
public boolean keyUp(int keycode) {
    if (keycode == Keys.LEFT)
        controller.leftReleased();
    if (keycode == Keys.RIGHT)
        controller.rightReleased();
    if (keycode == Keys.Z)
        controller.jumpReleased();
    if (keycode == Keys.X)
        controller.punchReleased();
    if (keycode == Keys.C)
        controller.throwReleased();
    if (keycode == Keys.D)
        renderer.setDebug(!renderer.isDebug());
    return true;
}

@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    message= "keyTyped was detected";
    Gdx.app.log("INFO", message);
    return false;

}


/// all below methods are for touch based game play
@Override
public boolean fling(float velocityX, float velocityY, int button) {
    if(Math.abs(velocityX)>Math.abs(velocityY)){
        if(velocityX>0){
            controller.throwPressed();
            return true;
        }else{

        }
    }else{
        if(velocityY<0){
            controller.jumpPressed();
            return true;
        }else{   
            controller.punchPressed();
            return true;
        }
    }
    return false;
}

@Override
public boolean touchDown(int x, int y, int pointer, int button) {
    /*if (!Gdx.app.getType().equals(ApplicationType.Android))
        return false;*/
    if (x < width / 3 && y > height / 2) {
        directionPointer= pointer;
        if(x< width/6){     
            controller.leftPressed();
            return true;
        }else{
            controller.rightPressed();  
            return true;
        }       

    }else{
        actionPointer= pointer;

    }
    return false;

}

@Override
public boolean touchUp(int x, int y, int pointer, int button) {

    if ( pointer==directionPointer) {           
        controller.leftReleased();
        controller.rightReleased(); 
        return true;                
    }
    return true;

}
}

【问题讨论】:

  • 您如何区分“向左或向右移动”和“扔星星”?是屏幕的那些不同区域(屏幕上有虚拟操纵杆之类的东西)吗?或者你在映射不同的动作(点击是移动,滑动是扔)?还是别的什么?
  • 嘿,是的,要检测向左或向右移动的手指必须位于屏幕的右下三分之一处,要检测跳跃、投掷星或冲头滑动动作,必须在屏幕上除左下三分之一以外的任何地方进行跨度>

标签: java android libgdx multi-touch


【解决方案1】:

嘿,所以我已经了解了为什么当另一根手指在屏幕上时我无法检测到甩动(在我的情况下,让角色移动)。原因是 fling 不是一个启用多点触控的事件,它仅检测是否只有事件被手势检测器触发。 因此,我目前正在修改逻辑以使用 touchdown 和 pan 、 panstop 事件,以便处理多点触控感应。我将用代码更新我如何实现它以及它可能导致的一些错误。对于将来遇到同样问题的其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多