How to load the previous level in my game instead of the next one?

Viewed 162

I am trying to create a game where you have to move a ball on a platform. At the start of the game, the ball is dropped on the platform and then you can move it around with the arrow keys. This is al working, no problems here.

The game starts with a ball dropped on a yellow tile as can be seen in my fiddle (click the 1 to start). The platform is build in javascript as follows (for level 1 and 2):

var levels = [
    [
        [2,1,1,1],
        [1,1,1,12],
        [1,1,1,1]
    ],
    [
        [2,1,1,1],
        [1,1,1,1],
        [1,1,12,1]
    ],
    [
        [2,1,1,1],
        [1,1,1,1],
        [12,13,1,1]
    ]
];

Where 1 = green tile, 0 = no tile, 2 = yellow start-tile, 12 = blue end-level-tile and 13 = red g0-level-back-tile.

If you click the 1 in the fiddle, you can move the ball with your arrow keys. If you move the ball on the blue tile, you will go to the next level. When this happens, the next level is loaded and the text Level: 1 will become Level: 2. All works great. From level 2 you can get to level 3 by moving the ball on the blue tile again.

In level 3 you see a red tile. At the moment this tile has the same functionality as the blue tile, but I want this to change. Instead of going to the next level, I want to go to the previous level when moving on the red tile. How to do this?

Some code explanation:

In the code below //Tile object I have the following code:

switch (type) {
        //end level
        case 12:
            this.ends = true;
            break;
        //level back NEW
        case 13:
            this.ends = true;
            break;
    } 

In case the blue tile (12) ends becomes true.

In my function isOn I declare what has to happen if false becomes true:

this.isOn = function (x, y) {
    var isOnIt = this.active && this.x == x && this.y == y;
    if (isOnIt) {
        if (this.ends) {
            ball.end(this.x, this.y);
        }
        return true;
    }
    return false;
};

So I have to add tile 13 (red tile) in here too like:

    this.isOn = function (x, y) {
        var isOnIt = this.active && this.x == x && this.y == y;
        if (isOnIt) {
            if (this.ends) {
                ball.end(this.x, this.y);
            }
            if (this.lvlback) {
                ball.gobacklvl(this.x, this.y);
            }
       

     return true;
    }
    return false;
};

And of course in the //tile object part add: this.lvlback = false;

and change:

//level back NEW
        case 13:
            this.ends = true;
            break;
    } 

to

//level back NEW
        case 13:
            this.lvlback = true;
            break;
    } 

So far so good. Now I need to add a function lvlback which loads the previous level and set the Level: 3 text back to Level: 2 (if you are in level 3 of course)

Note: You can also directly load level 3 by clicking the 3 at start. Also this way you should be able go back to the previous level (nr2). I was thinking of getting the current level and deduct 1 from it and then load the level using getLevel:

getLevel: function() {
            return level;
        }

I've also tried:

gobacklvl: function() {
            level --; //decuct level
            if (level == levels.length) {
                level = 0;
            }
            $("#level").text('Level: '+(level+1));
            plane.loadLevel(level);
        }

but both failed. I hope someone can figure out how to do this properly

Many thanks

1 Answers
Related