0
\$\begingroup\$

Right now this method goes through a list of GameObjects (e.g. JetPack, Platform) with their respective x and y positions in a text file called level1.txt:

platform 840 280
platform 1130 280
coin 900 300
coin 950 300
coin 1000 300
coin 1000 400
jetpack 1090 400
platform 650 500
platform 500 400
platform 200 400
NEXT
platform 200 200
coin 250 300
coin 350 700  

What currently happens is whenever my Player reaches the end of the screen, he goes back to the start (x = 0). When he does reach the end of the screen, I want all the objects BEFORE NEXTto disappear and all the objects AFTER NEXT to be drawn. I know why my algorithm doesn't work as it should but I'm unsure how to fix it. Can anyone have a look at it and tell me how I can modify it to do what I want it to? listis a list of GameObjects in the text file btw.

public void loadLevel(String level) {
        list.clear();
        FileHandle file = Gdx.files.internal(level);
        StringTokenizer tokens = new StringTokenizer(file.readString());
        while (tokens.hasMoreTokens()) {
            String type = tokens.nextToken();

            if (type.equals("platform")) {
                list.add(new Platform(Integer.parseInt(tokens.nextToken()),
                        Integer.parseInt(tokens.nextToken())));
            }
            else if (type.equals("coin")) {
                list.add(new Coin(Integer.parseInt(tokens.nextToken()), Integer
                        .parseInt(tokens.nextToken())));
            } else if (type.equals("flag")) {
                list.add(new Flag(Integer.parseInt(tokens.nextToken()), Integer
                        .parseInt(tokens.nextToken())));
            } else if (type.equals("jetpack")) {
                list.add(new JetPack(Integer.parseInt(tokens.nextToken()),
                        Integer.parseInt(tokens.nextToken())));
            } else if (type.equals("pipe")) {
                list.add(new Pipe(Integer.parseInt(tokens.nextToken()), Integer
                        .parseInt(tokens.nextToken())));
            } else if (type.equals("NEXT") && player.getxPos() > SCREEN_WIDTH) {
                list.clear();
            }
        }
    }

Any help is highly appreciated, many thanks in advance.

\$\endgroup\$
1
\$\begingroup\$

The problem is here:

} else if (type.equals("NEXT") && player.getxPos() > SCREEN_WIDTH) {

Granted that loadLevel is run at the start of your game, it will run and finish well before player.getxPos() is off the screen width. It will only check once when the level is initially loaded.

You need to be able to 'stop' reading the file at 'next', and be able to continue where it left off, when appropriate. You will also need to check every once in a while for when the player's x position is off the screens width, and when that happens that's when you resume the level load.

For me, I would store the text file at a string array (or better a queue), then go through the list of commands, parse them as needed and run them until you hit next or other 'flow control' command. Then later, when the player does something that continues the load process, the level first is 'cleared' then loading continues reading where it left off and runs more commands and the process repeats as needed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.