1

(Warcraftのように)クリックしたときにプレーヤーをマウスに移動させるにはどうすればよいですか?

これまでに試しました:

if (Mouse.isButtonDown(0)) {

    if (X < Mouse.getX()) {
        X += Speed;
    }
    if (X > Mouse.getX()) {
        X -= Speed;
    }
    if (Y < Mouse.getY()) { 
        Y += Speed;
    }
    if (Y > Mouse.getY()) {
        Y -= Speed;
    }
} 

しかし、それは私がマウスを押したままにした場合にのみ私が望むことをします。

4

1 に答える 1

3

ラストクリックの位置を保存し、プレーヤーをその方向に移動させるだけです。

次のフィールドをプレーヤークラスに追加します。

int targetX;
int targetY;

updateメソッドで、新しいターゲットを保存し、移動を適用します。

// A new target is selected
if (Mouse.isButtonDown(0)) {

    targetX = Mouse.getX();
    targetY = Mouse.getY();
}

// Player is not standing on the target
if (targetX != X || targetY != Y) {

    // Get the vector between the player and the target
    int pathX = targetX - X;
    int pathY = targetY - Y;

    // Calculate the unit vector of the path
    double distance = Math.sqrt(pathX * pathX + pathY * pathY);
    double directionX = pathX / distance;
    double directionY = pathY / distance;

    // Calculate the actual walk amount
    double movementX = directionX * speed;
    double movementY = directionY * speed;

    // Move the player
    X = (int)movementX;
    Y = (int)movementY;
}
于 2013-02-01T23:35:16.223 に答える