Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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 1042 times)
0 Members and 1 Guest are viewing this topic.
Offline konflikt

Junior Newbie





« Posted 2011-10-07 20: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:
http://pastebin.com/u/konflikt
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #1 - Posted 2011-10-07 20: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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
 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,
1  
2  
3  
4  
5  
6  
7  
8  
9  
 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!

Offline konflikt

Junior Newbie





« Reply #2 - Posted 2011-10-07 21: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00

Cero (60 views)
2014-07-16 00:42:17

Riven (57 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!