I’m trying to move a sprite around a tiled map where stone and water are solid. When my player sprite collides with a tile he stops moving and won’t move as long as he is colliding. How do I fix this code to allow horizontal movement during vertical collisions and allow vertical movement during horizontal collisions? Also, thanks for helping out in advance
// move player based on directions
// methods for controlling movement
public void move(){
playerRect.x += xDir;
playerRect.y += yDir;
}
public void checkCollision(){
for(int i = 0; i < world.mapSize; i ++){
if(playerRect.intersects(world.blocks[i]) && world.isSolid[i] ){
intersection = (Rectangle) playerRect.createIntersection(world.blocks[i]);
resolveCollision();
}
}
}
public void shoot(){
// figure this out later
}
private void resolveCollision(){
if(xDir > 0){
playerRect.x -= xDir;
}
if(xDir < 0){
playerRect.x -= xDir;
}
if(yDir > 0){
playerRect.y -= yDir;
}
if(yDir < 0){
playerRect.y -= yDir;
}
}