Home > Enterprise >  How to progress game to next level
How to progress game to next level

Time:10-04

For my project I am designing a game that has three levels, each set out as an array. Upon completing each level, the game should automatically progress to the next level. I currently have it progressing from the 1st to 2nd level, but upon completing level 2, it simply reloads levels 2 again. My teacher says I should be using a variable to determine my current level, and incrementing it as I progress to the next level, which I am doing, so I'm unsure of why the game is stuck on level 2. He mentioned I should be using a static variable but I don't understand how that differs from what I'm doing currently.

{
    private char[][] level1 = {
        {'#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' '},
        {'#', ' ', '|', '#', ' ', ' ', ' ', ' ', ' ', ' '},
        {'#', ' ', ' ', '#', '#', '#', ' ', ' ', ' ', ' '},
        {'#', ' ', '@', ' ', ' ', '#', ' ', ' ', ' ', ' '},
        {'#', ' ', ' ', '$', ' ', '#', ' ', ' ', ' ', ' '},
        {'#', ' ', ' ', '#', '#', '#', ' ', ' ', ' ', ' '},
        {'#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', ' '}
    };
    
    private char[][] level2 = {
        {'#', '#', '#', '#', '#', ' ', ' ', ' ', ' '},
        {'#', '@', ' ', ' ', '#', ' ', ' ', ' ', ' '},
        {'#', ' ', '$', '$', '#', ' ', '#', '#', '#'},
        {'#', ' ', '$', ' ', '#', ' ', '#', '|', '#'},
        {'#', '#', '#', ' ', '#', '#', '#', '|', '#'},
        {' ', '#', '#', ' ', ' ', ' ', ' ', '|', '#'},
        {' ', '#', ' ', ' ', ' ', '#', ' ', ' ', '#'},
        {' ', '#', ' ', ' ', ' ', '#', '#', '#', '#'},
        {' ', '#', '#', '#', '#', '#', ' ', ' ', ' '},
    };
    
    private char[][] level3 = {
        {'#', '#', '#', '#', '#', '#', ' '},
        {'#', ' ', ' ', ' ', ' ', '#', ' '},
        {'#', ' ', '#', '@', ' ', '#', ' '},
        {'#', ' ', '$', '*', ' ', '#', ' '},
        {'#', ' ', '|', '*', ' ', '#', ' '},
        {'#', ' ', ' ', ' ', ' ', '#', ' '},
        {'#', '#', '#', '#', '#', '#', ' '},
    };

public void act() {
\\game code here

if (testLevelComplete() == true) {
            levelComplete();   
        }
        
    }
   
    
    public void levelComplete()
    {
        int currentLevel = 0;
        currentLevel = currentLevel   1;
        showWarning("Level Complete! Current level is "   currentLevel);
        if (currentLevel == 1) {
            loadLevel(level1);
        } else {
        if (currentLevel == 2) {
            loadLevel(level2);
        } else {
         if (currentLevel == 3) {
            loadLevel(level3);
        } else {
        if (currentLevel > 3) {
            showWarning("Level Complete! No more levels.");
        }}}}
        
        
    }
    
}```

CodePudding user response:

When you are checking for the next level, you are restarting to 0, then re-add 1 because the currentLevel variable is include in method.

I suggest you to do something like that :

private int currentLevel = 0; // make the variable global
// to update always the same and not restarting all time

public void levelComplete() { // your method
   currentLevel  ; // go to next level
   showWarning("Level Complete! Current level is "   currentLevel);
   if (currentLevel == 1) { // set lvl1
       loadLevel(level1);
   } else if (currentLevel == 2) { // set lvl2
       loadLevel(level2);
   } else if (currentLevel == 3) { // set lvl3
       loadLevel(level3);
   } else if (currentLevel > 3) { // completed
       showWarning("Level Complete! No more levels.");
   }
}

Also, instead of doing else { if(...) } prefer use else if(...)

CodePudding user response:

When you call the levelComplete method, the variable currentLevel will be assign to 0 again.
If you want to keep the variable state, you should set the currentLevel variable as the member variable of your class.

CodePudding user response:

Like in most main-stream programming languages, variables are scoped in Java. Usually the curly braces { start or end a scope.

public void levelComplete()
{
    int currentLevel = 0; // starts living here
    // ... your code ...
} // `currentLevel` dies here and is gone

So every time you call levelComplete, you start at zero.

Since the level is part of you game state, you should move it up to be a property of your Game Object. These properties live as long as your object does, e.g.:

public class Game {
    // Lives as long as the containing Game object does
    private int level = 0;

    public void levelComplete() // shouldn't this rather be private?
    {
        this.level  ;
    }
}
  • Related