Hey guys, first post. I'm developing a game using LibGDX and it is going quite well!I took Java in college, but my previous game dev experience is with C/C++ (and a library called Allegro).

My game is similar to the star stage of Spore, but it is just a wide open star field. Stars are generated randomly as the player moves into new space using a predictive algorithm so that the same stars are always generated.

The problem I have is with this tremendously ugly piece of code that is central to my game:

The 9 blocks make up a 1440x1440 pixel "active map". Each StarMap_block contains a node list of stars, which are added to the stage.This function is called each time the camera moves out of the current active blocks.

What is a better approach to accomplish this? Is this leaking memory? Is this actually not a bad way to do what I need to? Do I honestly need to constantly cast shorts like that in Java?

I'm at a point where I need to add more into this code, but am looking for some feedback before I shoot myself in the foot.

Thanks for any help.

[edit]I posted a little demo video if anyone is interested in checking it out:

No - "short one = 1;" would be fine. Obviously defining a constant like this is a bad idea but I see why you did it. Just don't.

1

newStarMap_block((short)(center_x-one),(short)(center_y+one), stage)

Again no. As long as this constructor expects shorts then you don't need to cast to shorts. If the constructor expected int s or long s or something then Java would automatically convert them to that type. byte s are another matter.

Also for your information "StarMap_block" is not a Java class name convention. In Java we do camel case starting with capital for class names. Letters and numbers only. No underscores. So I would change it to StarMapBlock. Or you can make it a static nested class (define the class inside another class and make it static) in which case it would be referenced as "StarMap.Block" or you can import it and just reference it as "Block." I know your coming from C and you aren't used to it but if you are looking for help on a Java forum, it annoys some people when they see stuff like this. Also "embrace the native culture" and stuff.

In the interest of correctness (since we're there anyway) your resource names are slightly wrong.

In science a superscript number after an element refers to an isotope of that element.

So U232 is Uranium-232, the isotope of Uranium with a relative atomic mass of 232. Likewise with He3. Those are fine.

The problem is that the "2" in H20 and O2 is referring to the molecular formula and so it should be done with subscript. "H2O" and "O2". Also if you are going to refer to Oxygen as O2 you should probably refer to Hydrogen and Nitrogen as H2 and N2 as they are generally diatomic molecules as well.

When I do that it errors out and says it cannot convert from integer to short. I did some googling, and it seemed that java programmers had just "accepted" that.

Likewise with "center_x-one" (or center_x-1). Because the - operator only works with integers, the result of a short - short is an integer for some reason, which it could not convert back to a short. I was yelling at my compiler (IntelliJ IDEA) for a while trying to figure out why I needed those casts. Eventually I just gave up and put them in.

Quote

you can make it a static nested class (define the class inside another class and make it static) in which case it would be referenced as "StarMap.Block"

That actually sounds interesting, I didn't know one could do that

My primary concern with this code was with re-allocating the blocks continually into the same variable. Is that OK in Java?ie.StarMapBlock block = new StarMapBlock();block = new StarMapBlock();block = new StarMapBlock();block = new StarMapBlock();

The old ones get cleaned up ok?

Quote

The problem is that the "2" in H20 and O2 is referring to the molecular formula and so it should be done with subscript. "H2O" and "O2". Also if you are going to refer to Oxygen as O2 you should probably refer to Hydrogen and Nitrogen as H2 and N2 as they are generally diatomic molecules as well.

And http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html there you go. Can be extremely useful stuff. Also I don't mean to patronize but in case you don't know about it, read about anonymous classes (there's a C thing called something like that - I can't remember exactly what - but don't be fooled, they are not the same thing) because when I learnt Java that wasn't even on the tutorial and I didn't know about them for several weeks. It made understanding other peoples code so much harder.

Just wanted to hop into here and talk about things that you might think are interesting:

What java does is implicit conversion it converts the types of numbers implicitly.The opposite of that would be explicit type conversion, which is needed in strongly typed languages, for example haskell:

1 2 3 4 5 6 7 8 9 10

// Java code:intsomething = 0;floatohItsAFloatNow = something;

// haskell:something :: Intsomething = 0

ohItsAFloatNow :: FloatohItsAFloatNow = fromIntegralsomething

Here,

fromIntegral

is something like the cast

(short)

in java (except it works for every number type that an 'Int' can be converted to).

So, in haskell there are no implicit conversion. But implicit conversion is by no means a bad thing. For example in the programming language scala implicit conversions play a big role and are a big part of the language.

But implicit conversion can be done in a bad way, too.Say, for example, we want to pre-concatenate the number '5' in front of the string "37" in javascript. We might have some code like this:

1 2 3

varmyNumber = 5;varmyString = "37";varmyResult = myNumber + myString;

In this case,

myResult == 42

, did you get why?

Since "+" was used together with a number (5 from 'myNumber'), the string 'myString' was implicitly converted to a number.

Hope this was at least a little interesting and for you people reading this topic and maybe inspired you into learning a couple of new languages, there are so many out there and programming languages is quite an interesting topic!

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