I’m programming a 2D tile based game in Java like Bomberman (my first game).
I use a 2D array to represent the map where I keep track of walls, walkable spaces, etc. By now I was able to make the player move smoothly swaping sprites depending on the direction key he presses (but not interacting with the background/map, it can walk randomly and get out of the screen).
My problem comes when I want to keep track of the player’s position in the array. Logically the character always belongs to only one tile but graphically it can be between two tiles. How can I solve this issue?
I already tried this:
currentTileArrayX = x / tileWidth;
currentTileArrayY = y / tileHeight;
Where x and y are the top-left coordinates of the sprite.
But everything became very buggy because the player’s sprites aren’t the same size as the tiles.
I had another option in mind that would solve the previous problem: When the player presses a direciton key, move the character to the next tile like an animation. This wouldn’t let the player stop in the middle of two tiles… But what if he interrupts it with another key press? How can I stop user’s action while the animation is executing? I think this is the easiest solution but I don’t know how to implement it.
I already tried to find the solution in other questions but I had no luck. I hope you can help me.
Thanks a lot.
Aaaaaaaaaaaaaaah! Encountering the ‘discrete space problem’ 🙂 Your logic map is discrete but your UI movements are in the continuous space. I don’t the user can actually be in both tiles “at the same time” – visually yes, but never ‘internally’. Now you are getting the position of the player and that can be “anywhere” within the tile or “on the boundary” of 2 tiles so to speak…
You have two solutions to this problem, IMO:
if(currentPosition - boundary <= 0.000000001)then assume it’s the next tile else stay on the same one.movement speed defined and move the character that many steps per
‘click’/’button press’ – but you say you have the problem of key
pressed in “between”. I’m guessing if you code is designed like this
then it shouldn’t be a problem:
update() { calculate(); //update all datastructures/calculations draw(); //update the visual representation based on calculations }update()is a.k.a, gameLoop. I think this should solve your problem…