As tonyg says you dont stop the user from pressing the key but rather set the movement to zero if that movement will cause a collision. From what I am make out from your post your problem is when the player is moving diagonally for example up and to the right and hits a wall at the top you want the player to continue sliding along the wall to the right.
One possible solution could be:
Say you have the two variables PlayerX, PlayerY which have the players position. You would then have two more variables PlayerMoveX, PlayerMoveY which are reset to zero before you check for player input.
Then based on what keys the player is pressing you would set the movement variables accordingly.
So if the player is pressing the up key and the right key then PlayerMoveX would equal 1 and PlayerMoveY would equal -1.
Now you can check to see if the movement would cause a collision.
The trick here is to check each axis seperately.
First check if the movement in the x direction would cause a collision and if it does then reset the x movement.
If checkCollision(playerX + playerMoveX, playerY ) = true Then
playerMoveX = 0
End If
Now check in the yDirection
If checkCollision(playerX, playerY + playerMoveY ) = true Then
playerMoveX = 0
End If
Finally you can now set the new position of the player
playerX :+ playerMoveX
playerY :+ playerMoveY
Hope this is what you were after and it helps.
Cheers
One possible solution could be:
Say you have the two variables PlayerX, PlayerY which have the players position. You would then have two more variables PlayerMoveX, PlayerMoveY which are reset to zero before you check for player input.
Then based on what keys the player is pressing you would set the movement variables accordingly.
So if the player is pressing the up key and the right key then PlayerMoveX would equal 1 and PlayerMoveY would equal -1.
Now you can check to see if the movement would cause a collision.
The trick here is to check each axis seperately.
First check if the movement in the x direction would cause a collision and if it does then reset the x movement.
If checkCollision(playerX + playerMoveX, playerY ) = true Then
playerMoveX = 0
End If
Now check in the yDirection
If checkCollision(playerX, playerY + playerMoveY ) = true Then
playerMoveX = 0
End If
Finally you can now set the new position of the player
playerX :+ playerMoveX
playerY :+ playerMoveY
Hope this is what you were after and it helps.
Cheers