The
getPosition() method returns a constant reference you will need to use
setPosition(positionToMove);
The reason I suggest the method I did is because right now you go out of bounds then when the key is let up it goes back in bounds (hitting walls) and you currently go through objects.
By the way the code I was suggesting was just pseudo code not for you to copy/paste into it. Now you have a position and a positionToMove variable. You only need one.
The thing i was trying to mention is you have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
sf::Vector2u currWSize = window.getSize();
if(position.x <= 0)
{
position.x = 0;
}
if(position.y <= 0)
{
position.y = 0;
}
if(position.x >= currWSize.x - characterSprite.getGlobalBounds().width)
{
position.x = currWSize.x - characterSprite.getGlobalBounds().width;
}
if(position.y >= currWSize.y - characterSprite.getGlobalBounds().height)
{
position.y = currWSize.y - characterSprite.getGlobalBounds().height;
}
|
for checking collision with the walls but..it is after you move not before you move. Put it before
characterSprite.setPosition(position);
Again you are trying to set the position twice as well.
P.S. you can check the direction it is moving based on the velocity when I say
//change the new position now basically if you are moving right that means you hit something on the left so the x position should be the other objects x - the width of that object. So something like
position.x = otherPosition.x - widthOfObjectThatCollided;
P.P.S You can also just check the side that it was hit and make a custom collision detection instead of the
.intersect() method I will work on an example though might be a little while since I haven't used SFML in quite a while.