So, I managed to make my player on my map animate and move smoothly using the arrow keys (I even took a suggestion from a thread here to use booleans to fix ‘sticky movement’) I decided to see if I could implement some AI movement. I was able to generate a path for the player to follow (Using A* or BFS)
So far, I’m able to make the player follow the generated path, however, getting the animation to work is a little bit tricky.
Here are the the approaches I already took:
-Using the Robot class to press the keys (I was really hoping this would work, but it didn’t work as I hoped it would)
-Tried calling the setDely() and setDelX() methods (This approach also didn’t work due to the boolean method I took to fix the sticky movement)
-So, my current approach involved changing the booleans and calling the setDelX()/setDelY() methods, this is working for left to right animation, but it’s not doing so well for right to left animation. (Still trying to figure out why it’s behaving this way.)
Here’s the code for my current approach (I apologize for the comments lol) :
public void moveAlongPath(TileMap map, Path path) {
for(int count = 0; count < path.getLength(); count++)
{
Node step = path.getStep(count);
//System.out.println("Row: " + map.getRow(this.getY()) + " Col: " + map.getCol(this.getX()));
//TODO Smoother diagonal movement.
if(step.getRow() > map.getRow(this.getY()))
{
//Ensures all other movement is false.
isUp = false;
isLeft = false;
isRight = false;
setDelX(0);
isDown = true;
setDelY(2);
return; //Fixes path following bug.
}
/*
* PATH FOLLOWING BUG:
* Changing this 'if' to 'else if' fixes
* the path following for BFS, but
* A* Search path following is wrong
* Changing it to just 'if' fixes A* path
* following, but BFS path following is wrong.
*
* FIX: Adding 'return' fixed this error.
*/
if(step.getCol() > map.getCol(this.getX()))
{
//Ensures all other movement is false.
isUp = false;
isLeft = false;
isDown = false;
setDelY(0);
isRight = true;
setDelX(2);
return; //Fixes path following bug.
}
// if(step.getCol() < map.getCol(this.getX()))
// {
// isUp = false;
// isRight = false;
// isDown = false;
// setDelY(0);
//
// isLeft = true;
// setDelX(-2);
// return;
// }
// if(step.getRow() < map.getRow(this.getY()));
// {
// isRight = false;
// isLeft = false;
// isDown = false;
// setDelX(0);
//
// isUp = true;
// setDelY(-2);
// return;
// }
}
}
I’m just wondering what suggestions you guys would have or what approaches you would take to solve this problem. If you guys want to see some more code, let me know, thanks!