Help with Grid Movement and Collisions

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

Let’s start chipping away at what I’m reading.

Started off with Entity. That seems like a good idea, subclassing critters from a base class.

Then I started looking at Player. Here some problems arise immediately:


 public void moveDown(){
                        if(movableDown == true){
                                if(firstTime){
                                        a.removeAllScenes();
                                        for(byte i = 1; i >= 0; i--){
                                                a.addScene(image[i], 150);
                                        }
                                        firstTime = false;
                                }
                                sprite.setVelocityY(1);
                        }
                        else{
                                stopDown();
                        }
                }

Having something like ‘firstTime’ in movement code is no good. Do initializations in an initialization function, movement should be just movement and nothing else.
The byte addScene structure is not at all clear to somebody else who is reading the code. Try to clean it up and make it more human readable, anybody should be able to understand what is going on from clear variable names and comments if necessary. Do not name anything just ‘a’ one letter, give everything a full clear name.
The animation system seems odd, but I won’t get into that. No matter what, the call should just be animation.runWalkAnimation(direction) or something similarly clear.

The nullpointer will come because you are messing with your animation, adding and removing scenes, so when you call getHeight, the a class instance does not have any image at that moment I would think. Learn to use the debugger to trace precisely at what point the code crashes and see the values of the variables and classes that are being used at that moment.

Finally,


 if(keyCode == KeyEvent.VK_W && player.movableUp == true && player.walking == false){
                                player.walking = true;
                                moveTo = (int) Math.round(player.getCoordY() - 1);
                                if(player.movableUp){
                                        while(player.getCoordY() > moveTo){
                                                player.moveUp();
                                        }
                                }
                        }

You seem to be updating the movement in the keyListener. This is not going to work. Set movingUp to true, then handle everything else in the update of the player.

Good on you still for getting things started, carry on!

Thanks for the tips! I’ll see if I can get things working better from making these changes.