Java LibGDX多点触控运动 [英] Java LibGDX Multi Touch Movement

查看:78
本文介绍了Java LibGDX多点触控运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出什么问题了?我正在使用此代码来移动两个玩家.但这是行不通的.Player1比player2快.我该如何解决?

What is the problem? I am using this code to movement for two players. But it is not working. Player1 is more quick than player2. How can I fix?

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

推荐答案

我跳出两个错误:

  1. 您在几个地方使用了 getY()而不是 getY(i),因此除了最近的指针外,其他任何指针的标准也混在一起一个.

  1. You used getY() instead of getY(i) in a couple of places, so the criteria are mixed up for any pointer besides the most recent one.

您将其全部包裹成一个循环,该循环运行20次,因此,您将每个播放器移动一帧最多20次,而不是每次移动一次.该运动应在循环外部应用.

You wrapped it all in a loop that runs 20 times, so you are moving each player 20 times a frame instead of one time each, at most. The movement should be applied outside the loop.

此问题比最初看起来要复杂,因为您必须处理拒绝屏幕两侧多余的手指的问题.假设指针1是屏幕上半部分的手指.如果指针2在上半部分的手指上,您要拒绝它,但如果它在下半部分,则要使用它移动播放器2.如果指针1和2都在上半部分,则您要接受指针3仅在其底部时,但如果指针1和2在相反的一侧,则始终要拒绝它.

This problem is more complicated than it initially looks, because you have to deal with rejecting extra fingers on either side of the screen. Suppose pointer 1 is a finger on the top half of the screen. If pointer 2 is a finger on the top half, you want to reject it, but if it's on the bottom half, you want to use it to move player 2. If pointers 1 and 2 are both on the top, you want to accept pointer 3 only if it's on the bottom but you want to always reject it if pointers 1 and 2 are on opposite sides.

此外,如果只有一个手指向下,但它滑过边界滑到另一侧,则要确保不要让它开始控制对方的玩家.

Furthermore, if only one finger is down, but it slides across the boundary to the other side, you want to be sure you don't let it start controlling the opposite player.

这是一种可能的策略.向下跟踪两侧的第一个指针,并在释放所跟踪的指针时将其重置.仅更新两个当前跟踪的指针的目标位置.

Here's one possible strategy. Track the first pointer down on each side and reset it whenever the tracked pointer is released. Only update target positions for the two currently tracked pointers.

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

这篇关于Java LibGDX多点触控运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆