Sign in to follow this  
Croc

Dynamic object initialisation

Recommended Posts

I am writing a tile engine in Java 1.4.2 but this is more of a design question, hence posting it in this forum. I have several classes all of which inherit from the superclass "Sprite" and I have a SpriteManager which keeps tracks of all Sprites, loads and manages their graphics and calculates intersprite collisions. One thing I wish to implement is the loading of an external XML file at runtime that contains information about each sprite (start position, velocity as a function of distance or time, graphics filename, animation indexes etc) and the creation of each Sprite at runtime. My problem lies in the fact that although they all inherit from one class, they are not all of the same class so simple dynamic object initialisation is not possible. The best solution I have come up with so far is the following:
for(int i = 0; i < numberOfTypeA; i++){
   Sprite foo = new SpriteTypeA(parameters supplied by reading in from file);
   arrayList.add(foo);
}
for(int i = 0; i < numberOfTypeB; i++){
   Sprite foo = new SpriteTypeB(parameters);
   arrayList.add(foo);
}
etc
This is messy once the number of types grows beyond the trivial case of a few. Thus, does anyone have any design ideas for a better implementation. - Oscar [smile]

Share this post


Link to post
Share on other sites
As I understand it, you do have the name of the Spriteclass (i.e. Type a/b/c whatever) in your xml-defintion, right?!

If that's the case, you can use the reflection API to construct the objects dynamically (so, indeed, here it gets Java specific :-)).

For example:

Suppose you get the following info from your xml-file:

HeroSprite x=15 y=20 z=0 (and some more parameters...)
EnemySprite x=80 y=110 z=90
...

so, what you can do is this:


try {
Class mySprite = Class.forName(spriteClassName);
Constructor con = myPage.getConstructor(<specify the type of the constructor here>);

Sprite newSprite = (Sprite) con.newInstance(<give the concrete parameters here>);
} catch (Exception e1) {
}



Hope that helps. You can find additional docs in the java.lang.reflect docs.

Share this post


Link to post
Share on other sites
Excellent! [smile]

That is exactly what I envisaged but having never had a need to use it, had no idea it existed and I couldn't find it using generic searches (my keywords were probably wrong).

Thanks again.

-Oscar [smile]

Share this post


Link to post
Share on other sites
Anytime! Glad I could be of assistance...

However, please be aware that all your constructors of your subclasses should be of the same type (same signature), so that you can easily construct them in the for-loop (or otherwise, you'd have to specify the types _and_ the concrete objects you'd like to pass to it into the xml-file... Might be a bit awkward :-)).

I'm curious, though: What kind of game are you writing? 2D? 3D? Any APIs? (I am (veeeery slowly) converting from C++ to Java and I'm still experimenting with various 3D approaches... It's getting better and faster in Java, but I'm still not yet fully "healed" of C++)...

Thanks,
Beren

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this