gpt4 book ai didi

java - 在 libgdx 中处理多点触控

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:51 24 4
gpt4 key购买 nike

好吧,我一直在尝试为我的 libgdx 游戏实现多点触控解决方案,但遇到了一些麻烦。基本上我在屏幕的左下象限有一个 dpad,它控制我的角色的方向,在屏幕的剩余部分想要处理滑动以使角色跳跃、猛击或向敌人扔东西。我最初考虑在 GestureListener 中使用 fling 但是后来我被告知多点触控不支持 fling 并且已经开始使用 pan 来检测滑动。我目前遇到的问题是,如果我在使用滑动时将手指放在屏幕上(播放器正在移动并试图跳跃),则传递给平移停止的指针似乎不正确(至少基于我的记录和理解的实现)无论如何,这里是下面的代码和指向 docs on gesture detector here 的链接如果有人可以帮助解释发生了什么,将不胜感激。

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
if (x < width / 3 && y > height / 2) {
directionPointer= pointer;
if(x< width/6){
controller.leftPressed();
message=" driection TOUCH DOWN POINTER IS: " + pointer + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
Gdx.app.log("INFO", message);
return true;
}else{oller.rightPressed();
message=" direction TOUCH DOWN POINTER IS: " + pointer + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
Gdx.app.log("INFO", message);
return true;

}

}else{
actionPointer= pointer;
down_x=x;
down_y=y;
message= "Pointer value is" + actionPointer;
}

message="TOUCH DOWN POINTER IS: " + pointer + "the action pointer -> " +actionPointer +"directionPointer->"+ directionPointer;
Gdx.app.log("INFO", message);
return false;
}
@Override
public boolean touchUp(int x, int y, int pointer, int button) {

if ( pointer == directionPointer) {
controller.leftReleased();
controller.rightReleased();
directionPointer=-1;
return true;
}
if (x < width / 3 && y > height / 2) {

controller.leftReleased();
controller.rightReleased();
directionPointer=-1;
message= "TOUCHUP pointer is ->"+ pointer + "actionPointer->"+ actionPointer + "directionPointer-> "+ directionPointer;
Gdx.app.log("INFO", message);
return true;
}

message= "touchUP was detected";
Gdx.app.log("INFO", message);
return false;

}

@Override
public boolean panStop(float x, float y, int pointer, int button) {
message="swipe not processed";
if( pointer==actionPointer){
delta_x= Math.abs(down_x -x);
delta_y= Math.abs(down_y - y);
if (delta_x < delta_y ){
// this is an up or down value
if(down_x > x){
controller.bobPunch();
message = "SWIPE DOWNWARD " ;
}else {
// swipe up
controller.bobJump();
message = "SWIPE UPWARD " ;
}
}else{
if(down_y< y){
controller.throwPressed();
message=" SWIPE RIGHT";
//swipe right ward
}else{
controller.throwPressed();
message=" SWIPE LEFT";
// swipe left
}
}
}
Gdx.app.log("INFO", message);
message="panstop pointer is : " + pointer + "the action pointer-> " +actionPointer + "directionPointer->" + directionPointer;
Gdx.app.log("INFO", message);
actionPointer=-1;
// TODO Auto-generated method stub
return true;
}

现在这是检测应该采取什么行动的相关代码(directionPointer 和 actionPointer 是全局整数初始化广告 -1)问题是 panStop 返回的指针与我在这里预期的不一样是一些针对不同触摸操作的输出日志文件,以显示正在发生的事情:

情况 1:按住屏幕左下角(鲍勃向左移动):

direction TOUCHdOWN POINTER IS: 0 action pointer->-1 directionpointer->0

case2 向上滑动(bob 跳跃)

TOUCHdOWN POINTER IS: 0 action pointer->0 directionpointer->-1
SWIPE UPWARD
panstop pointer is: 0 actionpointer->0 directionpointer->-1

案例3移动和跳跃(这里是问题所在):

direction TOUCHdOWN POINTER IS: 0 action pointer->-1 directionpointer->0
TOUCHdOWN POINTER IS: 1 action pointer->1 directionpointer->0
swipe not processed
panstop pointer is: 0 actionpointer->1 directionpointer->0

现在最后一行虽然我不能向你证明是问题,因为我平移的手指被注册为 Action 指针,方向指针(不离开屏幕)是返回的指针。谁能指出我做错了什么,或者这是 libgdx 本身的错误,或者更有可能是我的代码本身

`

最佳答案

您实际上可以做的是让 DPAD 成为连接到舞台的 Actor ,而屏幕控件由 InputProcessor 或 GestureListener 处理,并使用 InputMultiplexer 将它们组合起来:

InputMultiplexer im = new InputMultiplexer(stage, inputProcessor);
Gdx.input.setInputProcessor(im);

此外,如果我没记错的话,发送到 InputMultiplexer 的参数的顺序算作优先级。这意味着在上述情况下,舞台 (DPAD) 将优先于屏幕控制。因此,在您的情况下,您可能想要切换它们。

我知道这对你来说可能不是一个好的解决方案,但我在我的一些游戏中一直这样做。

关于java - 在 libgdx 中处理多点触控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25146456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com