I’m fairly new to Java, and I’m trying to make an RPG for my own learning purposes. I am trying to make it move on a grid (à la Pokemon grid movement), but I am having trouble with this grid movement and collisions as well. I am wondering if someone could help explain to me the best ways to do grid movement and collisions, or how I can fix the code I have right now. Like I said before, I’m fairly new to Java so my code is probably not the most efficient. But any help would be greatly appreciated!
I’ll try to explain the problems I am having now. The grid movement semi-works… if I tap on a key to move the player, it will correctly move 16 pixels (that is my grid size) and then stop. But if I hold the key and then release, it won’t move exactly to a grid coordinate (for example it would move to an X coordinate of like 4.25 instead of moving to 4.00).
Collisions are also a problem. If I tap the movement key a few times to slowly approach an NPC, it will detect the collision. But if the key is held down, it will not to detect the collision. Also, sometimes whenever it tries to detect the collision, the game crashes and gives me a “java.lang.NullPointerException”, saying it is from a method that gets the height of the sprites image. For some reason, something messes up when getting the height of a sprite in my collision check method.
I hope this all makes sense, and if anyone could either help explain what I am doing wrong, or explain efficient ways of doing grid movement like the Pokemon games and collisions, that would be great!
Here is my source code:
http://pastebin.com/u/konflikt