![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
I have a few questions regarding some good ways to use sprites. Has anyone ever played the game Littlefighter2? (its written in C++ i think) It has these text files with something along the following.
Frame 1 pic:1 goto:2 wait:3 dx:0 dy:0 ifkeya:4 ifkeyb:10 ifkeyc:42 Frame 2 pic:2 goto:3 wait:3 dx:0 dy:0 ifkeya:5 ifkeyb:10 ifkeyc:20 Frame 3 pic:3 goto:1 wait:3 dx:0 dy:0 ifkeya:5 ifkeyb:10 ifkeyc:20 ... ...this continues for hundreds of frames... ... My question is what would be a good way to have a setup like above (in java)? Would it be better to have text files like this or have it all in the program? And how would I go around doing this or structure it (what would the classes/functions look like)? I would really appriciate any answer, thanks in advance. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
your program definatly can extract the info from a text file like this (check out class StreamTokenizer)
Text files are inefficient, however, and i dont particularly like how just anyone can go and mess around with them. Another possibility would be to create a class to store the information, save the data as objects to a file, and load it at start. Your class would probably look somthing like: public class SpriteData implements Serializable {
private int pic, goto, wait, dx, dy, ifkeya, ifkeyb, ifkeyc;
//etc, etc, etc
}And then when you start the program you could load all of these objects onto a Vector or somthing, for easy access. (For writing/reading objects to a file, check out class ObjectInputStream and ObjectOutputStream) Last edited by ZenMasterJG; Feb 19th, 2005 at 2:30 AM. Reason: Screwed up my code tags. Damn you nyquil. |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Heh. Funny you should ask. I'm making a Mario game right now, where everything (items, objects, backgrounds, enemies) except Mario himself is being read from a text file. I honestly believe it works pretty damned well, and it's probably far more organized, and changable than putting everything in your code.
I'll give you a little description of the way I have it set up, in case it helps you out any. First of all, I have a canvas class, where I'm drawing everything. Then I have seperate classes for everything, such as the ground, enemies, bricks, background objects, Mario and so on, you can create them as the need arises. I also created an interface with all the methods I possibly thought I might need, and had all of my classes aside from Mario implement it. I used an ArrayList in my canvas class to hold any objects I might need to (such as the ground, enemies, background object, and so on). Now, as Mario moved along to the right, I used an instance of Scanner (new to 1.5) to read my text file. As it read, it added it to the ArrayList. Here's what my text file might look like: (this is level1.lvl) Ground 100 520 40 40 HARD NOT_ACTING ground.gif And then, from my code, I basically read it like this: // this is way above in my code
ArrayList <Elements> elements = new ArrayList();
Scanner levelReader = new Scanner(new File("level1.lvl"));
// now we skip a lot of lines
.
.
.
.
.
.
.
//this is a little segment of how I might read from the file and then initialize it
String [] tempData = levelReader.nextLine().split(" ");
if(tempData[0].equals("Ground")
{
int xPos = Integer.parseInt(tempData[1]);
int yPos = Integer.parseInt(tempData[2]);
int width = Integer.parseInt(tempData[3]);
int height = Integer.parseInt(tempData[4]);
boolean hard = NOT_HARD; // HARD and NOT_HARD are predefined constants;
if(tempData[5].equals("HARD"))
{
hard = HARD;
}
boolean acting = NOT_ACTING; // again, predefined constants;
if(tempData[6].equals("ACTING"))
{
acting=ACTING;
}
String imgName = tempData[7];
elements.add(new Ground(xPos, yPos, width, height, hard, acting, imgName));
}
// Check for other objects, such as enemies, blocks, stuff like that
.
.
.
.
.
.
.
.
for(int i = 0; i<elements.size(); i++)
{
// draw them onto the canvas
}That's basically how I read from the text file. Of course, I don't parse everything into its own variable usually, I just do it in the constructor call itself, but I was hoping that the variable names would show you what each of those numbers meant. Hard just means if Mario intersects it, whether it should stop, or it should just keep going through it. Acting is for enemies, or blocks that break. They'll have an act method. Ground of course, doesn't act, therefore it is NOT_ACTING. Yea, but it's pretty easy to read from the text file, and pretty effective, in my opinion.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower Last edited by Mjordan2nd; Feb 19th, 2005 at 10:21 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|