Reputation: 565
I am currently creating a Java 2D game within which I receive commands from the user to move the character up, down, left or right a certain amount of distance. I am currently using a for
loop through the user inputs and pass the string to the Player class which will check if the user input string matches one of the direction to move the character. When all of this is executed it appears as though the player has teleported to the end location. Is there a way for the character to move certain amounts of pixels until it has reached the target location to make it seem as though the player is naturally moving to the location.
This is the movePlayer function which is used to loop through the JTextFields
which contain the user's commands to move the player. The strings
from each text field is passed into another function: inputListener
.
public void movePlayer(){
for (int i = 0; i < userTextInput.size(); i++) {
inputListener(userTextInput.get(i).getText());
}
}
The inputListener
checks if the strings
entered by the user matches the movement type, and the initiates the appropriate method to move the character.
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();
}
}
This is where the x
and y
position of the character is set according to the method run from inputListener
public void moveCharacterUp(){
y -= moveSpeed;
}
public void moveCharacterDown(){
y += moveSpeed;
}
public void moveCharacterLeft(){
x -= moveSpeed;
}
public void moveCharacterRight(){
x += moveSpeed;
}
The run method for the Thread
which I am using.
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();
}
}
}
Upvotes: 2
Views: 1152
Reputation: 11132
The way I have done this in the past is by creating a targetX
and targetY
.
I then increment x
and y
until they equal targetX
and 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);
}
}
Upvotes: 1
Reputation: 424
Your program need to count time. That time value will be use to scale moveSpeed. Key is pressed and you fetch current time. When you are about to apply moveSpeed you have to fetch time once again and calculate difference with previous time. That difference will be your scale factor. Something like this:
private void inputListener(String Input){
lastTime = getTime();
}
public void moveCharacterUp(){
currTime = getTime();
y -= moveSpeed * (currTime - lastTime)
lastTime = currTime;
}
Upvotes: 0
Reputation: 2238
The speed is set by the variable moveSpeed. This is probably set to a higher value than what you want, so try changing that.
Upvotes: 0