问题是什么?我正在使用此代码移动两名玩家。但这是行不通的。 Player1比player2更快。我该如何解决?
for (byte i = 0; i < 20; i++) {maxDistance = 10 * Gdx.graphics.getDeltaTi
me();
if (Gdx.input.isTouched(i) && Gdx.input.getY()<= 400) {
player1TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player1TouchPosition);
}
player1Tmp.set(player1TouchPosition.x, player1TouchPosition.y).sub(player1Rectangle.x, player1Rectangle.y);
if (player1Tmp.len() <= maxDistance) {
player1Rectangle.x = player1TouchPosition.x;
player1Rectangle.y = player1TouchPosition.y;
} else {
player1Tmp.nor().scl(maxDistance);
player1Rectangle.x += player1Tmp.x;
player1Rectangle.y += player1Tmp.y;
}
if (Gdx.input.isTouched(i) && Gdx.input.getY() >= 401) {
player2TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player2TouchPosition);
}
player2Tmp.set(player2TouchPosition.x, player2TouchPosition.y).sub(player2Rectangle.x, player2Rectangle.y);
if (player2Tmp.len() <= maxDistance) {
player2Rectangle.x = player2TouchPosition.x;
player2Rectangle.y = player2TouchPosition.y;
} else {
player2Tmp.nor().scl(maxDistance);
player2Rectangle.x += player2Tmp.x;
player2Rectangle.y += player2Tmp.y;
}
}
最佳答案
我遇到两个错误:
您在几个地方使用了getY()
而不是getY(i)
,所以除了最近的指针以外,其他任何指针的标准都混在一起。
您将其包装成一个循环运行20次,因此将每个播放器移动一帧最多20次,而不是每次移动一次。该运动应在循环外部进行。
这个问题比最初看起来要复杂得多,因为您必须处理掉屏幕两侧多余的手指。假设指针1是屏幕上半部分的手指。如果指针2在上半部分的手指上,则要拒绝它,但如果它在下半部分,则要使用它来移动播放器2。如果指针1和2都在上半部分,则要接受指针3仅在其位于底部时,但是如果指针1和2在相反的一侧,则始终要拒绝它。
此外,如果只有一根手指向下,但它滑过边界移到另一侧,则要确保不要让它开始控制对方的玩家。
这是一种可能的策略。在两侧向下跟踪第一个指针,并在释放跟踪的指针时将其重置。仅更新两个当前跟踪的指针的目标位置。
private int player1Pointer = -1, player2Pointer = -1;
// render():
//stop tracking released fingers
if (player1Pointer >=0 && !Gdx.input.isTouched(player1Pointer))
player1Pointer = -1;
if (player2Pointer >=0 && !Gdx.input.isTouched(player2Pointer))
player2Pointer = -1;
//check for new pointers and update target positions
for (int i = 0; i<20; i++){
if (!Gdx.input.isTouched(i))
continue;
if (Gdx.input.getY(i) <= 400){ //bottom, player 1
if (player2Pointer == i)
continue; //player 2 slid finger across boundary, ignore it
if (player1Pointer < 0)
player1Pointer = i; //first finger down on this side of screen, track it
if (player1Pointer == i){ //this is the tracked finger, update target
player1TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player1TouchPosition);
}
} else { //top, player 2
if (player1Pointer == i)
continue;
if (player2Pointer < 0)
player2Pointer = i;
if (player2Pointer == i){
player2TouchPosition.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
camera.unproject(player2TouchPosition);
}
}
}
//update movement toward targets
maxDistance = 10 * Gdx.graphics.getDeltaTime();
temp.set(player1TouchPosition.x, player1TouchPosition.y).sub(player1Rectangle.x, player1Rectangle.y);
if (temp.len() <= maxDistance) {
player1Rectangle.x = player1TouchPosition.x;
player1Rectangle.y = player1TouchPosition.y;
} else {
temp.nor().scl(maxDistance);
player1Rectangle.x += temp.x;
player1Rectangle.y += temp.y;
}
temp.set(player2TouchPosition.x, player2TouchPosition.y).sub(player2Rectangle.x, player2Rectangle.y);
if (temp.len() <= maxDistance) {
player2Rectangle.x = player2TouchPosition.x;
player2Rectangle.y = player2TouchPosition.y;
} else {
temp.nor().scl(maxDistance);
player2Rectangle.x += temp.x;
player2Rectangle.y += temp.y;
}