Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with Grid Movement and Collisions  (Read 1239 times)
0 Members and 1 Guest are viewing this topic.
Offline konflikt

Junior Newbie

« Posted 2011-10-07 18:04:05 »

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:
Offline Karmington

Senior Devvie

Medals: 1
Projects: 1

Co-op Freak

« Reply #1 - Posted 2011-10-07 18:38:57 »

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){
                                        for(byte i = 1; i >= 0; i--){
                                                a.addScene(image[i], 150);
                                        firstTime = false;

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.

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

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!

Offline konflikt

Junior Newbie

« Reply #2 - Posted 2011-10-07 19:16:00 »

Thanks for the tips!  I'll see if I can get things working better from making these changes.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (35 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (43 views)
2015-07-27 16:38:00

Riven (24 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (39 views)
2015-07-25 00:14:37

BurntPizza (48 views)
2015-07-24 22:06:39

BurntPizza (33 views)
2015-07-24 06:06:53

NoxInc (40 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!