Java 游戏角色移动太快
我目前正在创建一个Java2D游戏,在这个游戏中,我接收用户的命令,将角色向上、向下、向左或向右移动一定距离。我目前正在使用Java 游戏角色移动太快,java,Java,我目前正在创建一个Java2D游戏,在这个游戏中,我接收用户的命令,将角色向上、向下、向左或向右移动一定距离。我目前正在使用for循环遍历用户输入,并将字符串传递给Player类,Player类将检查用户输入字符串是否与移动角色的方向之一匹配。当所有这些都被执行时,玩家似乎已经传送到了终点位置。有没有一种方法可以让角色移动一定数量的像素,直到它到达目标位置,从而让玩家看起来好像是在自然地移动到该位置 这是movePlayer函数,用于循环通过JTextFields,其中包含用户移动播放器的命令。
for
循环遍历用户输入,并将字符串传递给Player类,Player类将检查用户输入字符串是否与移动角色的方向之一匹配。当所有这些都被执行时,玩家似乎已经传送到了终点位置。有没有一种方法可以让角色移动一定数量的像素,直到它到达目标位置,从而让玩家看起来好像是在自然地移动到该位置
这是movePlayer函数,用于循环通过JTextFields
,其中包含用户移动播放器的命令。来自每个文本字段的字符串
被传递到另一个函数:inputListener
public void movePlayer(){
for (int i = 0; i < userTextInput.size(); i++) {
inputListener(userTextInput.get(i).getText());
}
}
这是根据从inputListener运行的方法设置字符的x
和y
位置的地方
public void moveCharacterUp(){
y -= moveSpeed;
}
public void moveCharacterDown(){
y += moveSpeed;
}
public void moveCharacterLeft(){
x -= moveSpeed;
}
public void moveCharacterRight(){
x += moveSpeed;
}
我正在使用的线程的run方法
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();
}
}
}
public void run(){
init();
长起点;
久违;
漫长的等待;
(跑步时){
start=System.nanoTime();
更新();
draw();
drawToScreen();
已用=System.nanoTime()-开始;
等待=targetTime-已用时间/1000000;
如果(等待<0)等待=5;
试一试{
线程。睡眠(等待);
}捕获(例外e){
e、 printStackTrace();
}
}
}
速度由可变移动速度设置。这可能设置为比您想要的更高的值,因此请尝试更改该值 您的程序需要计算时间。该时间值将用于缩放移动速度。键,您将获取当前时间。当您即将应用moveSpeed时,必须再次获取时间并计算与前一时间的差值。这种差异将成为您的比例因子。大概是这样的:
private void inputListener(String Input){
lastTime = getTime();
}
public void moveCharacterUp(){
currTime = getTime();
y -= moveSpeed * (currTime - lastTime)
lastTime = currTime;
}
我过去的做法是创建targetX
和targetY
然后我增加x
和y
,直到它们等于targetX
和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);
}
}
intx=0//角色的x位置
int y=0//角色的y位置
int targetX=0//角色的目标x位置
int-targetY=0//角色的目标y位置
int-moveSpeed=2//角色移动的速度
int moveAmt=20//每次通知角色移动时,将其设置为移动的量
void setTarget(int-targetX,int-targetY)//设置targetX和targetY,根本不需要调用
{
this.targetX=targetX;
this.targetY=targetY;
}
void moveCharacter(int x,int y)//移动字符,根本不需要调用
{
这个.x=x;
这个。y=y;
}
void updatePosition()//启动/继续移动,应每帧调用一次
{
if(输入等于(“向上”)){
setTarget(targetX,targetY-移动金额);
}else if(输入等于(“向下”)){
setTarget(targetX,targetX+moveAmt);
}else if(Input.equals(“left”)){
setTarget(targetX-移动金额,targetX);
}else if(Input.equals(“right”)){
设置目标(targetX+moveAmt,targetX);
}
如果(y>目标){
player.moveCharacter(x,y-移动速度);
}否则,如果(ytargetX){
player.moveCharacter(x-moveSpeed,y);
}否则如果(x
Thread.sleep(200)可能是一种(过于)简单的方法代码>或游戏循环中的类似内容。这不会改变角色移动的距离或速度,它只会在线程中创建一个轻微的暂停,我只能想象这会非常恼人?如果我将移动速度更改为20,例如向上移动20个像素,角色就会这样做,但是它会在瞬间完成,而且不会像一个人按下一个键那样缓慢。我是否应该像这样使用setTarget()进行设置:setTarget(x+=moveSpeed,y+=moveSpeed)?setTarget()
设置播放器将移动到的位置,updatePosition()
应该每帧调用一次,以使玩家向目标移动,并且根本不需要调用moveCharacter()。另外,moveSpeed
可能需要调整以满足您的需要。我认为您对这个问题有点误解。我不会根据按键事件移动角色。我将字符移动一定数量的像素,例如,对于updatePosition
接收到的每个命令,可以是up
,down
,left
或right
。即使在我实现你的代码时,角色也会移动,但它会立即移动,例如,不会一次移动两个像素,以使它看起来像在移动一样。@Human我只是稍微修改了代码。此外,Input
应设置为任意字符串,例如在每帧的开头设置“0”
。如果不这样做,可能会导致您的角色不断朝一个方向移动。@humanoops,没有看到您最后的评论。修改后的代码应该可以工作moveSpeed
是目标移动的速度,moveAmt
是当Input
设置为“up”
,“down”
等时目标移动的量。。
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);
}
}