I want to make an RPG game in which I move tile by tile. So when I hit up, the tile row that I am on decreases by one for example. Also, it's supposed to be a slow movement so that I can see the change in tile, i.e. I can see my sprite move from tile to tile. Currently, with the code I have, when I hit a direction on my keyboard, I move several blocks within seconds and by the time I release the button I have already gotten a nullPointerException error because I have left the map. How can I slow down the movement?
|
This sounds like an input problem. You need to check your inputs to see if you've already processed a key press. Keep the state of the keyboard from the last frame and check it when you're on your new frame. Only move if the key was not being pressed in the previous frame. Similar to responding to the event of pressing the key instead of the key being down. For example:
That will apply one move to each key press. If you wanted to have a held key repeat movements you can do something like:
Additionally, you should probably be checking bounds when moving. So check that your character's position + movement amount is withing world bounds, before moving. |
|||||||
|
|
Just a note on structure - you are calling repaint(); in your update(); function, but it would probably be better if you didn't chain the calls like that, but rather have those in your main game loop, ie.:
|
|||
|
|
|
This is the code I used.
|
|||||||
|