Ok. Here's how I load my tiles:
for(int i = 0; i < H; i++)
{
for(int j = 0; j < W; j++)
{
if(tileMap[i][j] == '0')// || tileMap[i][j] == ' ')
tile_ground.setTextureRect(sf::IntRect(256, 64, 32, 32));
if(tileMap[i][j] == 'P')
tile_ground.setTextureRect(sf::IntRect(512, 0, 32, 32));
tile_ground.setPosition(j * 32, i * 32);
window.draw(tile_ground);
}
}
And here's how I do the collision checking:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
source.y = Up;
isKeyPressed = true;
if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds())) // I check if the player sprite and the obstacle intersect
heroSprite.move(0, - heroSpeed.y * deltaTime);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
source.y = Down;
isKeyPressed = true;
if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds())) // I check if the player sprite and the obstacle intersect
heroSprite.move(0, heroSpeed.y * deltaTime);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
source.y = Left;
isKeyPressed = true;
if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds())) // I check if the player sprite and the obstacle intersect
heroSprite.move(- heroSpeed.x * deltaTime, 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
source.y = Right;
isKeyPressed = true;
if(!heroSprite.getLocalBounds().intersects(tile_ground.getLocalBounds())) // I check if the player sprite and the obstacle intersect
heroSprite.move(heroSpeed.x * deltaTime, 0);
}
But my sprite is stuck in only one tile and can't move in any direction. How do I prevent this?