2

我目前正在创建一个 Java 2D 游戏,在该游戏中,我收到用户的命令,将角色向上、向下、向左或向右移动一定距离。我目前正在使用for循环通过用户输入并将字符串传递给 Player 类,该类将检查用户输入字符串是否与移动字符的方向之一匹配。当所有这些都被执行时,玩家似乎已经传送到了结束位置。有没有办法让角色移动一定数量的像素,直到它到达目标位置,让玩家看起来好像是自然地移动到该位置。

这是 movePlayer 函数,用于循环JTextFields包含用户移动播放器的命令。来自strings每个文本字段的 被传递到另一个函数:inputListener.

public void movePlayer(){

    for (int i = 0; i < userTextInput.size(); i++) {
        inputListener(userTextInput.get(i).getText());
    }
} 

inputListener检查用户输入的是否与strings移动类型匹配,并启动适当的方法来移动角色。

private void inputListener(String Input){

     if(Input.equals("up")){
         player.moveCharacterUp();

     }else if(Input.equals("down")){
         player.moveCharacterDown();

     }else if(Input.equals("left")){
         player.moveCharacterLeft();

     }else if(Input.equals("right")){
         player.moveCharacterRight();

     }

}

这是根据运行方法设置字符的x和位置的地方yinputListener

public void moveCharacterUp(){
    y -= moveSpeed;
} 

public void moveCharacterDown(){
    y += moveSpeed;
}

public void moveCharacterLeft(){
    x -= moveSpeed;
}

public void moveCharacterRight(){
    x += moveSpeed;
}

Thread我正在使用的运行方法。

public void run(){

    init();

    long start;
    long elapsed;
    long wait;

    while(running){

        start = System.nanoTime();

        update();
        draw();
        drawToScreen();

        elapsed = System.nanoTime() - start;
        wait = targetTime - elapsed / 1000000;

        if(wait < 0) wait = 5;

        try{
            Thread.sleep(wait);

        }catch(Exception e){
            e.printStackTrace();
        }

     }
    }
4

3 回答 3

1

我过去这样做的方法是创建一个targetXand targetY
然后我递增xandy直到它们等于targetXand targetY

int x = 0; //Character's x position
int y = 0; //Character's y position
int targetX = 0; //Character's target x position
int targetY = 0; //Character's target y position
int moveSpeed = 2; //the speed at which the character moves
int moveAmt = 20; //amount the character is set to move every time it is told to move

void setTarget(int targetX, int targetY) //sets targetX and targetY, doesn't need to be called at all
{
    this.targetX = targetX;
    this.targetY = targetY;
}

void moveCharacter(int x, int y) //moves the character, doesn't need to be called at all
{
    this.x = x;
    this.y = y;
}

void updatePosition() //initiates/continues movement, should be called every frame
{
    if(Input.equals("up")) {
         setTarget(targetX, targetY - moveAmt);

    } else if(Input.equals("down")) {
         setTarget(targetX, targetX + moveAmt);

    } else if(Input.equals("left")) {
         setTarget(targetX - moveAmt, targetX);

    } else if(Input.equals("right")) {
         setTarget(targetX + moveAmt, targetX);

    }

    if(y > targetY) {
         player.moveCharacter(x, y - moveSpeed);

    } else if(y < targetY) {
         player.moveCharacter(x, y + moveSpeed);

    } else if(x > targetX) {
         player.moveCharacter(x - moveSpeed, y);

    } else if(x < targetX) {
         player.moveCharacter(x + moveSpeed, y);

    }
}
于 2013-10-25T17:57:44.507 回答
0

速度由变量 moveSpeed 设置。这可能设置为比您想要的更高的值,因此请尝试更改它。

于 2013-10-25T17:07:35.640 回答
0

你的程序需要计算时间。该时间值将用于缩放 moveSpeed。按键被按下,您获取当前时间。当您即将应用 moveSpeed 时,您必须再次获取时间并计算与前一次的差异。这种差异将是您的比例因子。像这样的东西:

private void inputListener(String Input){
   lastTime = getTime();
}

public void moveCharacterUp(){
   currTime = getTime();
   y -= moveSpeed * (currTime - lastTime)
   lastTime = currTime;
} 
于 2013-10-25T17:48:00.813 回答