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  
  How to internally save objects properly (this subject might be misdirecting)  (Read 240 times)
0 Members and 1 Guest are viewing this topic.
Offline Ecen

Junior Newbie


Exp: 2 years


Magic, is science that we don't yet understand.


« Posted 2013-05-16 18:36:09 »

Hello!

I'm pretty much just starting out with java, but I've learnt most basics by now. I think.

Anyways, I've just started out on a somewhat simple RPG-style game in which the player is supposed to be able to do  rpg-generic things like moving and fighting. Now, I've drafted some kind of basic layout for the program. One part handles drawing and one handles the actual game data. In the part that is supposed to handle the object-object interaction I've run into somewhat of a problem, and it's this problem that forms my slightly confused question:

How do I create objects and save them to be able to paint them later on while still retaining full control of specific objects?

To clarify what I mean, my last project involved a ball and a few lines. I essentially 'saved' internally them like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
private Ball ball;
private MyLine[] line = new line[10]

public ConstructorOfThisClass(){

ball = new Ball(position, size)
line[0] = new MyLine(x1, y1, x2, y2)
line[1] = new MyLine(x1, y1, x2, y2)
line[2] = ...
}


To draw this I just had to call the ball's draw method as well as the draw method of line[ i ] in a loop cycling through all lines for every frame.

In this rpg, there must be more than 2 kinds of things. For example I may want a few trees, the player, a house, a couple of stones and an enemy. All of these things can of course subclass some kind of overall WorldObject class and then get their unique properties in their own class files. But how is it advised that I handle them? I kinda feel that manually (or in a loop with random values) creating a whole lot of tree instances and saving them to a list somewhere is not such a good solution. Perhaps some kind of grid?

I feel that I want to be able to refer to any one object at any time as well as being able to handle them all at once, mostly for drawing them.

So, yeah, anyone got any tips or any guides it might be worthwhile for me to look at? If you want me to clarify something, please tell, as it is obvious that I might not have made my point quite clear.

Cheers!
Offline dermetfan

Senior Member


Medals: 11



« Reply #1 - Posted 2013-05-16 19:03:51 »

You mostly have to create them all manually, unless you save them in a file or class or generate them. You can add them to some kind of container, though.

A common way to do this is the stage and actor system.
Let's say you have some classes like Ball, Tree, Rat, I don't know. They all extend Actor. Then you can add them to a stage like this:
1  
2  
3  
Stage stage = new Stage();
Ball ball = new Ball(position, size);
stage.addActor(ball);


This way, you could add all your actors to an ArrayList for example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Stage {
    private ArrayList<Actor> actors = new ArrayList<Actor>();
    ...
    public void addActor(Actor actor) {
        actors.add(actor);
    }
    public ArrayList<Actor> getActors() {
        return actors;
    }
    ...
}


To draw them you could use an enhanced for loop:
1  
2  
for(Actor actor: stage.getActors())
    actor.draw(); // or whatever way you draw your things

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.

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

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

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

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

Riven (39 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
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!