Making a 2d game, need help with border around the screen
Ok, so I and a friend is making a small java game. Currently it' sonly a little man moving across whole the screen(up, down, left and right).
I won’t to know what the best way to have the character not moving outside some borders will be? So he couldn't like walk over a house.
Currently I check if the player is outside the screen when i update the position, what will be the easiest way to have like boxes on the screen that he couldn’t go inside?
Thanks
EDIT: Corrected typos.
Re: Making a 2d game, need help with border around the screen
Hello and welcome to the forum.
Please if you get a chance, edit your original post to correct typos and spelling mistakes. I've found that if posts are easier to read, more folks will read them -- which is what you want, right?
Regarding your problem, this is a problem of your program logic. You'll need to have a non-gui representation of boundaries and obstacles and a non-GUI representation of the little man's position, and use this to determine where the man can and can't go. The specifics will depend on your program's structure.
Re: Making a 2d game, need help with border around the screen
Ok, fixed typos =).
For the problem; How would the best way to save the obstacles be?
currently for the screenborders i have:
Code:
if(Main.Busthus.getX() + Main.Busthus.getWidth()+1 >= s.getWidth() && m.right){
Main.Busthus.setMoving(false);
m.right = false;
}
if(Main.Busthus.getX()-1 <= 0 && m.left){
Main.Busthus.setMoving(false);
m.left = false;
}
if(Main.Busthus.getY()-1 <= 0 && m.up){
Main.Busthus.setMoving(false);
m.up = false;
}
if(Main.Busthus.getY() + Main.Busthus.getHeight()+1 >= s.getHeight() && m.down){
Main.Busthus.setMoving(false);
m.down = false;
}
s is my screen, Busthus is the little man,m and Main is my mainclass.
Re: Making a 2d game, need help with border around the screen
Quote:
Originally Posted by
tobial
Ok, fixed typos =).
For the problem; How would the best way to save the obstacles be?
I don't know of any "best way", but when I've done something like this before, I've used a simple text file with letters to represent the grid such as g for grass and h for house, and then have the program read this into an array. Then when moving the man, check his position relative to my data array to see if he's trying to go somewhere there's an obstacle, and if so, not allow it.
Re: Making a 2d game, need help with border around the screen
aha, so putting every pixle of the obstacle in a array? And checking if the mans position is in the array?
Re: Making a 2d game, need help with border around the screen
Quote:
Originally Posted by
tobial
aha, so putting every pixle of the obstacle in a array? And checking if the mans position is in the array?
Again, this is just one way. You also could have an ArrayList of Obstacle type, a class that describes where an obstacle is located and that has a method boolean contains(Point p), or something similar, and then simply iterate through this list when moving your man. There are many ways to skin this cat.
Re: Making a 2d game, need help with border around the screen
Ok, i'll try that
I will try to have a class for House. and in the class iwill have a arraylist with points, but it it any way to automaticly fintd the points between 10:10 and let say 30:45?