Something is wrong in the moving section of my game engine and I just can't figure out what it is. Maybe you guys can help.
I'm using java2D and the game is basically JFrame->JPanel->Canvas. It is tile based with "smooth" movement between the tiles. Now to the problem:
Moving in different directions seems to go at different speed. Moving left is approx. twice as fast as moving right. Moving up is twice as fast as moving down. I am compensating for that right now by adjusting the speed for the different direction but I just can't figure out why this is happening. Every movement is basically using the same parameters and only the direction is different.
Sample code of how the movement is done.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private void moveLeft(int delta) { if (x - delta * .1f <= targetX) { x = targetX; } else { x -= delta * .1f; } }
private void moveRight(int delta) { if (x + delta * .1f >= targetX){ x = targetX; } else { x += delta * .1f; } } |
The following is called when the left key is pressed. No moving is actually done here. Only setting up the destination, the sprite for the direction and telling update() via
moving that there is moving to be done.
1 2 3 4 5
| direction = DS.LEFT; current = DS.playerLeftSprite; moving = true; targetX = x - DS.tileSize; targetY = y; |
And respectively for the right key.
1 2 3 4 5
| direction = DS.RIGHT; current = DS.playerRightSprite; moving = true; targetX = x + DS.tileSize; targetY = y; |
I will leave out the up and down movement since it's basically the same on the y-axis.
The
moveLeft() and
moveRight() methods are called during each update as long as the entity has not reached it's destination. Delta is also provided from the update() method.
DS is my data storage for several constants and stuff. So besides the direction there is no difference between moving left and right.
I'm really at a loss right now. I can adjust the code so the speed is the same for all directions but I want to know why this weird stuff is happening.
Hopefully you guys can shed some light on this.
Thanks in advance.
PS: if you need additional code please let me know