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 (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  [SOLVED]Saving and Loading  (Read 1305 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Posted 2014-01-10 17:25:50 »

I am currently making a game where players can make buildings. However, when i was playing with saving and loading i stubbled accross a weird problem. Objects save fine but when they are loaded they sort of go invisible. However when i check the data by calling all the buildings in the system to check their positions and their names everything looks fine so yeah im kinda confused :/ . One thing you might want to know before looking at the code, "buildings" is an Arraylist and takes three parameters, i.e. new Building(string, int, int);. Here is the code i use for saving and loading buildings:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
public static void Save_Buildings() {
      try {

         // saving
        FileOutputStream saveFile = new FileOutputStream("saves/World_Data.sav");
         ObjectOutputStream save = new ObjectOutputStream(saveFile);

         save.writeObject(Object_Control.Buildings.size());
         
         for (int i = 0; i < Object_Control.Buildings.size(); i++) {
            save.writeObject(Object_Control.Buildings.get(i).name);
            save.writeObject(Object_Control.Buildings.get(i).x);
            save.writeObject(Object_Control.Buildings.get(i).y);
            }
         for (int i = 0; i < Object_Control.Buildings.size(); i++) {
            System.out.println(Object_Control.Buildings.get(i).name);
            System.out.println(Object_Control.Buildings.get(i).x);
            System.out.println(Object_Control.Buildings.get(i).y);
            }
         save.close();
         saveFile.close();
         
      } catch (Exception exc) {
         exc.printStackTrace();
      }
   }  
   public static void Load_Buildings() {
      try {
         FileInputStream saveFile = new FileInputStream("saves/World_Data.sav");
         ObjectInputStream save = new ObjectInputStream(saveFile);
         
         Object_Control.Buildings.clear();
         
         int AmountOfBuildings = (Integer)save.readObject();
         
         for (int i = 0; i < AmountOfBuildings; i++) {
            Object_Control.addBuilding(new Building("",0,0));
         }
         
         for (int i = 0; i < Object_Control.Buildings.size(); i++) {
            Object_Control.Buildings.get(i).name = (String) save.readObject();
            Object_Control.Buildings.get(i).x = (Integer) save.readObject();
            Object_Control.Buildings.get(i).y = (Integer) save.readObject();
            }
         
         for (int i = 0; i < Object_Control.Buildings.size(); i++) {
            System.out.println(Object_Control.Buildings.get(i).name);
            System.out.println(Object_Control.Buildings.get(i).x);
            System.out.println(Object_Control.Buildings.get(i).y);
            }
         save.close();
         saveFile.close();
         Menu.loading=false;
         
      } catch (Exception exc) {
         exc.printStackTrace();
      }
   }

I love making games...
Offline trollwarrior1
« Reply #1 - Posted 2014-01-10 17:36:29 »

Genius Coder doesn't realize that nobody can really help you with such issues. This is not a library usage bug, like LWJGL. This is the code you written buggy. This is only loading part. You might be doing something wrong in a different place entirely.

The problem might be that you're actually assigning values that you use for rendering ONLY after some kind of an event. But seriously, nobody can really help you.

PS

People should stop doing something, just because they think they can be the best at it. Like "Genius Coder". You obviously are not a genius and you will never be. I remember when I started playing Counter Strike 1.6 like when I was 10 years old, first time I connected to server, my username was "killer", even though I couldn't kill anyone. This seems like a thing that humans use to hide their weakness. In reality I was a noob at that game who couldn't do anything, so I used words to try to make an impression. You're doing the same here. I'm not blaming or anything, just something I noticed about human beings Cheesy
Ow just to add it, if you don't know what League Of Legends is, it is basically a match game (like CoD) where you play matches about 30 minutes or something. You can play a lot of different characters. There are people who only "Main" 1 character and it is like they "think" they are good at it, because they "main" it. In reality, they are usually the worst player in the team. Sad

I think I should stop this shiet. Way too off topic Cheesy
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2014-01-10 17:40:12 »

^ Pretty much. The name is kinda obnoxious; I could care less but it's impossible to tell what the problem is from the limited code you've provided.

[offtopic]
@trollwarrior Maining a champion actually works, if you put the time into it. But yeah, autolock teemo is kinda annoying.
[/offtopic]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #3 - Posted 2014-01-10 17:43:44 »

Just something I would like to point out, I didnt choose my name my frined did so it cant be much of a weekness can it. I also realised that I had limited the code here, thats because I'm making a game to sell so I cant release a load of source code, anyway what would help you solve the problem. In other words what do you need to see?

I love making games...
Offline trollwarrior1
« Reply #4 - Posted 2014-01-10 17:47:00 »

I don't think you got what we said.

The problem might be anywhere in your source code. ANYWHERE. From my little experience, I would suggest you look into constructors, type changes. You should first of all do a rough search for the bug. Meaning, don't stare at the same code for a long time. Just look if it has bugs or not, if not, check other parts of the code. Don't limit yourself to one area of the code. Just look at as much code as possible. BUGS ARE AT PLACES YOU DON'T EXPECT THEM TO BE. So just LOOK ANYWHERE. It is like "don't think, just do it".

Also, about the commercial thingy. Did you ever do a commercial project before?
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2014-01-10 17:47:14 »

Just something I would like to point out, I didnt choose my name my frined did so it cant be much of a weekness can it. I also realised that I had limited the code here, thats because I'm making a game to sell so I cant release a load of source code, anyway what would help you solve the problem. In other words what do you need to see?
Ok you shouldn't be trying to sell a game if you're new to game development. And even then releasing source code is the least of your issues. You can give us that snippet of code, or you can solve the problem by yourself. You're talking of invisible objects, but you're not even giving us the code that renders.
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #6 - Posted 2014-01-10 17:54:08 »

Quote
Ok you shouldn't be trying to sell a game if you're new to game development. And even then releasing source code is the least of your issues. You can give us that snippet of code, or you can solve the problem by yourself. You're talking of invisible objects, but you're not even giving us the code that renders.

Sorry I expected you to sort of guess

1  
2  
3  
      for (Building c : Buildings) {
         c.draw(g2d);
      }

I love making games...
Offline Dxu1994
« Reply #7 - Posted 2014-01-10 17:55:09 »

Quote
Ok you shouldn't be trying to sell a game if you're new to game development. And even then releasing source code is the least of your issues. You can give us that snippet of code, or you can solve the problem by yourself. You're talking of invisible objects, but you're not even giving us the code that renders.

Sorry I expected you to sort of guess

1  
2  
3  
      for (Building c : Buildings) {
         c.draw(g2d);
      }


Yup cause we all have crystal balls here and we know what your draw method does  Roll Eyes

Also, your coding style is terrible. Please read & learn existing Java programs before trying to write your own.

Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #8 - Posted 2014-01-10 17:55:14 »

Quote
Also, about the commercial thingy. Did you ever do a commercial project before?

I beleive not no

I love making games...
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #9 - Posted 2014-01-10 17:56:24 »

Quote
Yup cause we all have crystal balls here and we know what your draw method does

Well I mean its obvious enough so dont be a sarcy little shit because im seriously not in the mood

I love making games...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #10 - Posted 2014-01-10 17:56:54 »

Are you for real mate?

Seriously.. If this is your FIRST PROJECT, you should just share it with everyone for free. g2d probably means "graphics 2d" which means your game is probably shiet. If this is your FIRST COMMERCIAL PROJECT, make it free also, you will soon realize that nobody will buy your game. You can trust me on that one. I thought I was making the best game. 2 people bought it for 2EUROS each. I could call it a success. You need tremendous amount of work for people to enjoy it. You always need to put in 10 times more work than you're putting in.
Offline jmguillemette
« Reply #11 - Posted 2014-01-10 17:58:15 »

The problem is likely one of the following

An order of events issue.
or
A scope issue (scope as in what variables can be referenced and from where)

Your drawing works..  you showed this by drawing the buildings initially.
But your loading doesnt.. so what is different in these flows?

All of this i am summarizing based on your description of the problem not your code.

j.

-=Like a post.. give the author a medal!=-
Offline Dxu1994
« Reply #12 - Posted 2014-01-10 17:58:21 »

Honestly if I didnt know better i'd just assume this guy's an elaborate troll.

Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #13 - Posted 2014-01-10 17:59:40 »

Are you for real mate?

Seriously.. If this is your FIRST PROJECT, you should just share it with everyone for free. g2d probably means "graphics 2d" which means your game is probably shiet. If this is your FIRST COMMERCIAL PROJECT, make it free also, you will soon realize that nobody will buy your game. You can trust me on that one. I thought I was making the best game. 2 people bought it for 2EUROS each. I could call it a success. You need tremendous amount of work for people to enjoy it. You always need to put in 10 times more work than you're putting in.

Im really not in the mood mate so if your not here to help with the problem like I have very kindly requested at the start of the thread then please leave and if i want to sell a game, whether it'll be a succes or not I honestly dont care

I love making games...
Offline Sethir

Senior Newbie


Exp: 3 years



« Reply #14 - Posted 2014-01-10 18:00:41 »

Did you use BufferedImages to display them? If so you should know that these are transient and doesn't get saved.
Save the Path to the image and get it back when you load. Something like
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
   
   public void setBattlerSprite(String filePath) {
      this.spritePath = filePath;
      this.battlerSprite = new Sprite(filePath);
   }
   
   public Sprite getSprite() {
      if(this.battlerSprite == null) {
         this.battlerSprite = new Sprite(spritePath);
      }
     
      return this.battlerSprite;
   }
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #15 - Posted 2014-01-10 18:01:42 »

@Genius Coder
Everyone here has been trying to help you. But you need to post more code. If you want help I recommend you actually listen to the people who are trying to help you, who are better at coding than you.
Offline Dxu1994
« Reply #16 - Posted 2014-01-10 18:11:52 »

Here i'll try and help you now, but you honestly need to post more code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
class Building implements Serializable {
    int x, int y;
    String path;
    transient BufferedImage wot;

    public void draw(Graphics2D g) {
        if (wot == null) wot = loadImage(path);
        g.drawImage(wot, x, y, null);
    }
}

then to save:

ObjectOutputStream oos = ..
oos.writeInt(buildings.size());
for (Building b : buildings) oos.writeObject(b);

then to load:

ObjectInputStream ois = ...
int zzzz = ois.readInt();
buildings = new ArrayList(zzzz);
for (int i = 0; i < zzzz; i++) buildings.add((Building)ois.readObject());

Offline Longarmx
« Reply #17 - Posted 2014-01-10 18:40:42 »

In your buildings constructor do you do any kind of image initialization? If so, then when you set the variables directly ie:
1  
2  
3  
Object_Control.Buildings.get(i).name = (String) save.readObject();
Object_Control.Buildings.get(i).x = (Integer) save.readObject();
Object_Control.Buildings.get(i).y = (Integer) save.readObject();

it will not have any effect. Why are you not just creating the objects when you load:
1  
2  
3  
for (int i = 0; i < AmountOfBuildings; i++) {
     Object_Control.addBuilding(new Building((String)save.readObject(), (Integer)save.readObject(), (Integer)save.readObject()));
}

Try this for all your loading code and see if it fixes it.

Could you post your building constructor code and initialization to help us debug it easier?

Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #18 - Posted 2014-01-10 22:12:42 »

I don't know if I missed something, but this guy is just like the next clueless newbie, yet for some reason, he is getting flamed off the mark, just because of his username.

He's gotten the general response these people normally get (learn to debug, don't bother selling your first game, if you want help, give relevant source, etc.), but the attitude has been a lot... darker. Undecided

On topic:

Learn to debug.
Don't bother selling your first game.
If you want help, we need the relevant source code.

Offline Slyth2727
« Reply #19 - Posted 2014-01-10 22:33:23 »

You obviously are not a genius and you will never be.

Shut the f**k up. Nobody tells someone that. Ever. How about I tell you that? You are a retard, a complete idiot, and you will never ever be the least bit smart. That didn't sound so great did it? Now I do not believe you are stupid, quite the opposite actually, but you need to think about how you would feel if when you first started programming someone told you that when you asked a question. Please do.

Excuse my language. I don't think it's acceptable for people to tell someone they are stupid or something of the like when they don't know them and just judge on what they first see. Please, next time think, "Will any good come out of me saying this or doing that?". Because saying that stuff just puts people down and makes you look like a dick.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #20 - Posted 2014-01-10 22:37:58 »

Its just because its frustrating to see so many people assume they are all that, and assume that selling a game is very easy, and assume that he doesn't have to show code because he's going to be a big shot developer. Genius Coder, we've already seen all the code you've written. Not yours specifically, but I can bet you everyone on this thread has been programming for longer. Thinking that we can't see your code because you're going to sell it is ignorant and offensive.

Offline Slyth2727
« Reply #21 - Posted 2014-01-10 23:38:44 »

Its just because its frustrating to see so many people assume they are all that, and assume that selling a game is very easy, and assume that he doesn't have to show code because he's going to be a big shot developer. Genius Coder, we've already seen all the code you've written. Not yours specifically, but I can bet you everyone on this thread has been programming for longer. Thinking that we can't see your code because you're going to sell it is ignorant and offensive.

Point taken, but I don't think that warrants a "You're not a genius and you won't ever be". And it is ignorant and offensive to think something like that, so he needs to learn that. I just hate when people tell somebody they can't do something that they totally can. That is ignorant and offensive as well.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #22 - Posted 2014-01-11 00:16:44 »

I agree, that's totally not warranted, but being ignorant is very annoying. I don't care who you are, not researching before you get into something makes you look foolish.

Offline Otreum

Junior Member


Medals: 6



« Reply #23 - Posted 2014-01-11 01:10:57 »

Off topic:
I don't think he is trolling. I saw the name and chuckled to myself, but lighten up guys, he's just like every other person on this forum, a programmer, who happens to be asking for suggestions/help.
I don't think his name warrants the amount of abuse he has copped from certain members here.
So I say let's stay on topic, stop the flaming, and try and help out, without being condescending.

On topic:
I, myself am no expert on loading/saving...in fact, I've just popped back onto the forums are a while of no programming, and first thing I have asked about is saving/loading Tongue

While I am not great with the syntax of saving and loading, the concept behind it is pretty straight forward. Just like storing food in the fridge so you can eat it later.

Just like everyone else has said so bluntly here, you haven't posted the core rendering code that may present the problem, and I understand it may be a problem for you if you wish to sell your game. But remember, most of the people who are able to provide you with help, are people who actually have made games themselves, or are more experienced programmers who have been programming for years on end, refining and expanding their own programming knowledge...one way to help learn programming is to share your existing knowledge with others, hence why forums like this exist. So my point is, you probably shouldn't worry too much about people stealing your code....generally people who have to steal your code, have no idea how to use it anyway Wink
You don't have to post the entire source, but in this case, your methods for rendering.

It sounds like you are loading the data into the game, object positioning and names etc are showing when called from within a List (I'm guessing that is what you are using to keep track of your entities?), but the objects themselves are not visually being rendered.
This tells me that the objects obviously exist in your world, but the information loaded, is not being used to determine what textures to load in the location of your objects.
You say that they work before saving, but not after loading.
So it's a matter of follow the yellow brick road and see where the bump is that takes things off course. I would say that your object information is being loaded into a List or ArrayList, but the code that normally determines what textures to load where those objects are, is not using that information at runtime.
It really is hard to tell without some sort of code snippet to show what is going wrong, and where. It sounds like something ridiculously simple, but it could also be ridiculously complicated and difficult if your code is poorly structured.

Offline Andre Lopes
« Reply #24 - Posted 2014-01-11 02:13:23 »



http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/tutorial.html
http://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
http://www.tutorialspoint.com/java/java_serialization.htm
http://www.tutorialspoint.com/java/java_serialization.htm

http://www.youtube.com/watch?v=YzwiuRDgSSY

http://www.youtube.com/watch?v=Sm9yoju1me0

Hope that helps.
Offline philfrei
« Reply #25 - Posted 2014-01-11 02:19:45 »

A possible debugging check would be to write a "dump" method that displays ALL the information held in the Buildings class. Then, compare before and after the saving/loading process.

Or use a step-debugger and inspection to verify this, by putting breakpoints before and after the load.

It is plausible that an inexperienced programmer might add a "transient" keyword on the suggestion of an IDE, if there were any unserializable objects held in the class being saved/loaded. If "transient" was used for any variables, the data held in them will not be restored in the load.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #26 - Posted 2014-01-11 11:52:07 »

I agree, that's totally not warranted, but being ignorant is very annoying. I don't care who you are, not researching before you get into something makes you look foolish.

what are you on about not researched it, I have otherwise how else did i know how to write up the save and load method, i came here because it wasnt working properly not becaujse i wanted loads of source code!

I love making games...
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #27 - Posted 2014-01-11 13:08:20 »

Here i'll try and help you now, but you honestly need to post more code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
class Building implements Serializable {
    int x, int y;
    String path;
    transient BufferedImage wot;

    public void draw(Graphics2D g) {
        if (wot == null) wot = loadImage(path);
        g.drawImage(wot, x, y, null);
    }
}

then to save:

ObjectOutputStream oos = ..
oos.writeInt(buildings.size());
for (Building b : buildings) oos.writeObject(b);

then to load:

ObjectInputStream ois = ...
int zzzz = ois.readInt();
buildings = new ArrayList(zzzz);
for (int i = 0; i < zzzz; i++) buildings.add((Building)ois.readObject());


I get an error here: for (int i = 0; i < AmountOfBuildings; i++)  Object_Control.Buildings.add((Building)save.readObject());
The system says:
java.lang.ClassCastException: java.lang.Integer cannot be cast to items.Building

I love making games...
Offline Genius Coder

Junior Member


Medals: 1
Exp: 4 years



« Reply #28 - Posted 2014-01-11 13:28:21 »

My Building class for anyone that still wants to help:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
public class Building  extends Entity implements Serializable{

   public Building(String name, int x, int y) {
      super(x, y);
     
      this.name = name;
   }
   
   public String name;
   public Image currentImage;
   
   //new Structures
  private Image ConcreteShack;
   private Image Tent;
   private Image BarbedWire;
   private Image Barricade;
   private Image Wire_Fence;
   private Image Wood;
   private Image Wasteland;
   private Image Plasma_Core;
   private Image base_platfrom;
   
   public static int velX = 0;
   public static int velY = 0;
   World W;
   
   public void update() {
      y += velY;
      x += velX;
   }
   public void draw(Graphics2D g2d) {
      Image();
      g2d.drawImage(currentImage, x, y, null);
      if (Game.debugMode) { g2d.draw(getBounds()); }
   }
   
   public void Image() {
      ImageIcon CS = new ImageIcon(this.getClass().getResource("/buildings/CH1.png"));
      ConcreteShack = CS.getImage();
      ImageIcon T = new ImageIcon(this.getClass().getResource("/placable_Items/tent.png"));
      Tent = T.getImage();
      ImageIcon BW = new ImageIcon(this.getClass().getResource("/placable_Items/Barbed_Wire.png"));
      BarbedWire = BW.getImage();
      ImageIcon Bar = new ImageIcon(this.getClass().getResource("/placable_Items/baracade2.png"));
      Barricade = Bar.getImage();
      ImageIcon WF = new ImageIcon(this.getClass().getResource("/placable_Items/Wire Fence.png"));
      Wire_Fence = WF.getImage();
      ImageIcon W = new ImageIcon(this.getClass().getResource("/placable_Items/wood.png"));
      Wood = W.getImage();
     
      ImageIcon WASTE = new ImageIcon(this.getClass().getResource("/buildings/wasteland.png"));
      Wasteland = WASTE.getImage();
      ImageIcon PC = new ImageIcon(this.getClass().getResource("/buildings/plasma core.png"));
      Plasma_Core = PC.getImage();
      ImageIcon BP = new ImageIcon(this.getClass().getResource("/backgrounds/NO TEXTURE.png"));
      base_platfrom = BP.getImage();
     
      if (name == "") { currentImage = base_platfrom;}
      if (name == "Concrete Shack") { currentImage = ConcreteShack;}
      if (name == "Tent") { currentImage = Tent;}
      if (name == "Barbed Wire") { currentImage = BarbedWire;}
      if (name == "Barricade") { currentImage = Barricade;}
      if (name == "Wire Fence") {currentImage = Wire_Fence;}
      if (name == "Wood") {currentImage = Wood;}
      if (name == "Plasma Core") {currentImage = Plasma_Core;}
      if (name == "Wasteland") {currentImage = Wasteland;}
      if (name == "Base Platform") {currentImage = base_platfrom;}
   }
   
   public Rectangle getBounds() {
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
     
      if (name == "Plasma Core") { return new Rectangle(x ,y , 150, 250);}
      if (name == "Wasteland") { return new Rectangle(x ,y , 80, 80);}
      if (name == "Wood") { return new Rectangle(x ,y , 25, 30);}
      if (name == "Wire Fence") { return new Rectangle(x ,y , 150, 20);}
      if (name == "Concrete Shack") { return new Rectangle(x ,y , 225, 205);}
      if (name == "Tent") { return new Rectangle(x ,y , 250, 200);}
      if (name == "Barbed Wire") { return new Rectangle(x ,y , 400, 10);}  
      if (name == "Barricade") { return new Rectangle(x ,y, 100, 135 - 90);} else
      return new Rectangle(x ,y , 0, 0);
   }
}

I love making games...
Offline Dxu1994
« Reply #29 - Posted 2014-01-11 13:32:47 »

My Building class for anyone that still wants to help:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
public class Building  extends Entity implements Serializable{

   public Building(String name, int x, int y) {
      super(x, y);
     
      this.name = name;
   }
   
   public String name;
   public Image currentImage;
   
   //new Structures
  private Image ConcreteShack;
   private Image Tent;
   private Image BarbedWire;
   private Image Barricade;
   private Image Wire_Fence;
   private Image Wood;
   private Image Wasteland;
   private Image Plasma_Core;
   private Image base_platfrom;
   
   public static int velX = 0;
   public static int velY = 0;
   World W;
   
   public void update() {
      y += velY;
      x += velX;
   }
   public void draw(Graphics2D g2d) {
      Image();
      g2d.drawImage(currentImage, x, y, null);
      if (Game.debugMode) { g2d.draw(getBounds()); }
   }
   
   public void Image() {
      ImageIcon CS = new ImageIcon(this.getClass().getResource("/buildings/CH1.png"));
      ConcreteShack = CS.getImage();
      ImageIcon T = new ImageIcon(this.getClass().getResource("/placable_Items/tent.png"));
      Tent = T.getImage();
      ImageIcon BW = new ImageIcon(this.getClass().getResource("/placable_Items/Barbed_Wire.png"));
      BarbedWire = BW.getImage();
      ImageIcon Bar = new ImageIcon(this.getClass().getResource("/placable_Items/baracade2.png"));
      Barricade = Bar.getImage();
      ImageIcon WF = new ImageIcon(this.getClass().getResource("/placable_Items/Wire Fence.png"));
      Wire_Fence = WF.getImage();
      ImageIcon W = new ImageIcon(this.getClass().getResource("/placable_Items/wood.png"));
      Wood = W.getImage();
     
      ImageIcon WASTE = new ImageIcon(this.getClass().getResource("/buildings/wasteland.png"));
      Wasteland = WASTE.getImage();
      ImageIcon PC = new ImageIcon(this.getClass().getResource("/buildings/plasma core.png"));
      Plasma_Core = PC.getImage();
      ImageIcon BP = new ImageIcon(this.getClass().getResource("/backgrounds/NO TEXTURE.png"));
      base_platfrom = BP.getImage();
     
      if (name == "") { currentImage = base_platfrom;}
      if (name == "Concrete Shack") { currentImage = ConcreteShack;}
      if (name == "Tent") { currentImage = Tent;}
      if (name == "Barbed Wire") { currentImage = BarbedWire;}
      if (name == "Barricade") { currentImage = Barricade;}
      if (name == "Wire Fence") {currentImage = Wire_Fence;}
      if (name == "Wood") {currentImage = Wood;}
      if (name == "Plasma Core") {currentImage = Plasma_Core;}
      if (name == "Wasteland") {currentImage = Wasteland;}
      if (name == "Base Platform") {currentImage = base_platfrom;}
   }
   
   public Rectangle getBounds() {
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
     
      if (name == "Plasma Core") { return new Rectangle(x ,y , 150, 250);}
      if (name == "Wasteland") { return new Rectangle(x ,y , 80, 80);}
      if (name == "Wood") { return new Rectangle(x ,y , 25, 30);}
      if (name == "Wire Fence") { return new Rectangle(x ,y , 150, 20);}
      if (name == "Concrete Shack") { return new Rectangle(x ,y , 225, 205);}
      if (name == "Tent") { return new Rectangle(x ,y , 250, 200);}
      if (name == "Barbed Wire") { return new Rectangle(x ,y , 400, 10);}  
      if (name == "Barricade") { return new Rectangle(x ,y, 100, 135 - 90);} else
      return new Rectangle(x ,y , 0, 0);
   }
}


Okay.

A few things:

1. You're reloading the image every single time draw() is called. Waste of CPU and memory. Just cache it and refer to the cached instance when you want to draw.

2. Why are you loading resources for EVERY building in the game in Image() ? You should only load the resources you NEED.

3. You can't compare strings with == , use .equals( .. )

4. Please use a better naming style, for example use aVariable instead of A_Variable.

5. What the hell is this?

1  
2  
3  
if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}
      if (name == "") { return new Rectangle(x ,y , 0, 0);}


And if you want help with the Load/Save, you'll need to post your Load/Save code.

Pages: [1] 2
  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.

CogWheelz (11 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (30 views)
2014-07-23 20:56:16
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!