I’m working on a detective game assignment using p5.js (as part of a “Sleuth” case study). The detective character moves through a city map based on if…else if conditions that adjust speedX and speedY.
Here’s a simplified version of my directional logic inside the draw() function:
The detective starts at (199, 10) and needs to follow a maze-like road toward the perp at (763, 696). However, in my current implementation, the character either stops moving or goes off-road at some point.
I’ve been trying to fix this by tweaking the coordinates in each condition, but something is still off — especially during transitions between horizontal and vertical movement.
How can I improve the logic or structure of my if…else if blocks to make sure the character follows the intended route without getting stuck or going off-road?
Any help or code structuring tips would be greatly appreciated!
if (det.locationY < 135 && det.locationX < 210) {
det.speedX = 0;
det.speedY = 1;
}
else if (det.locationY >= 135 && det.locationY < 138 && det.locationX < 702) {
det.speedX = 1;
det.speedY = 0;
}
else if (det.locationX >= 702 && det.locationY < 260) {
det.speedX = 0;
det.speedY = 1;
}
else if (det.locationY >= 260 && det.locationX > 268) {
det.speedX = -1;
det.speedY = 0;
}
else if (det.locationX <= 268 && det.locationY < 448) {
det.speedX = 0;
det.speedY = 1;
}
else if (det.locationY >= 448 && det.locationY <= 450 && det.locationX < 827) {
det.speedX = 1;
det.speedY = 0;
}
else if (det.locationX >= 827 && det.locationY < 670) {
det.speedX = 0;
det.speedY = 1;
}
else if (det.locationY >= 670 && det.locationX < 763) {
det.speedX = 1;
det.speedY = 0;
}
else if (det.locationX >= 763 && det.locationY < 696) {
det.speedX = 0;
det.speedY = 1;
}
enter code here



