The problem appears to be in your update method.
private void update(){
tempx = player.getX(); // tempx now is the same as the players x location
tempy = player.getY();
collided = checkCollision();
if(collided == false){
player.update();
}
else if(collided){
player.setX((int)tempx); // you set players location equal to temp, which is
player.setY((int)tempy); // already the players location
System.out.println("COLLIDED");
}
}
Since you are setting the players location equal to the location that it is currently in, you're not going to see the character move anywhere at all. You'll probably want to change the values of tempx
and tempy
tempx = player.getX() + 10;
tempy = player.getY() + 10;
Update
There seems to be some confusion about how the updating process is working.
Consider the following:
- character starts at (0,0)
- some object at (2,2) chich would cause a collision
Given the above the following happens in your update method and IN THIS ORDER
tempx = player.getX()
both x's are now 0tempy = player.getY()
both y's are now 0- checks collision, there are none
- since no collisions, player is updated. (I'll presume the update method moves the character (+1,+1). So character is now at location (1,1).
- tempx and tempy are set to getX and getY again. X and Y are now both 1. Now tempx and tempy are also 1
- no collisions, so character is updated (moved) to (2,2)
- tempx and tempy are set to getX and getY. X and Y are now both 2, so tempx and tempy are also 2.
- checks collisions, its true there was one at (2,2)
- because there was a collision you then "move" the character to tempx and tempy, BUT tempx and tempy are the same value as character.getX and character.getY.
You have to set tempx
and tempy
equal to the characters location outside of the update loop if you want them to remain the same throughout the characters updated movements.