0

I have recently migrated from JS and the Processing library to LibGDX with Java. In my time with JS, I have used code like this, for level design:

var level = [
    " aaaaaaaaa",
    " aaaaaaaaa",
    " aaaaaaaaa",
    " aaaaaaaaa",
    " aaaaaaaaa",
    "aaaaaaaaaa",
    "aaaaaaaaaa",
    "saaaggaaaa",
    "ggggccgggg",
    "cccccccccc",
    "aaaaaaaaaa",
    "aaaaaaaaaa",
    "cccccccccc"
];

With this, I made a for loop and switch like so:

for(var c = 0; c < level.length; c ++){ 
    for(var r = 0; r < level[c].length; r ++){
        switch(level[c][r]) {
            case 'a':
                rect(r*40, c*40, 40, 40);
                break;
            case 'g':
                ...
        }
    }
}

This checks through each character in each string in the array. Due to Java's more abstract system of variables and lack of interchangeable array and string properties, I have found it very difficult to do this same thing in Java. Does anyone know of a way that I can? Thanks in advance!

9
  • Arrays and the switch statement Commented Aug 5, 2015 at 23:56
  • 2
    I had loaded my "Java is not Javascript" gun but this time it's actually a question about both languages :D Commented Aug 6, 2015 at 0:05
  • By the way Java, as a language, is much better than Javascript and it does not lack anything JS could ever have. JS is a terrible language which happens to be the most used so people came up with lots of librairies which make it less terrible... but it is still a deeply flawed language.Be happy for having migrated Commented Aug 6, 2015 at 0:09
  • @Dici last time I tried Java in a web browser it had some serious problems :) Commented Aug 6, 2015 at 0:17
  • 1
    @Dici, Pointy, Nayuki-Minase: This thread is now officially a JavaScript-bashing thread. All further comments on this post that do not involve criticism of JavaScript will be considered off-topic as will be promptly removed as such. /s Commented Aug 6, 2015 at 2:01

1 Answer 1

4

It's very similar in Java.

String[] level = {
            " aaaaaaaaa",
            " aaaaaaaaa",
            " aaaaaaaaa",
            " aaaaaaaaa",
            " aaaaaaaaa",
            "aaaaaaaaaa",
            "aaaaaaaaaa",
            "saaaggaaaa",
            "ggggccgggg",
            "cccccccccc",
            "aaaaaaaaaa",
            "aaaaaaaaaa",
            "cccccccccc"
};
for(int c = 0; c < level.length; c ++){
    for(int r = 0; r < level[c].length(); r ++){
        switch(level[c].charAt(r)) {
            case 'a':
                // do something    
                break;
            case 'g':
                // etc   
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly! Thanks so much! :D
Can you help me again? I am wondering how to append a new instance of a certain class to an array that is meant to hold those instances in place of // do something, and then call the appropriate methods by looping through the array. Do you get what I'm trying to do? Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.