5

For one of my school projects, I have to create a game with Javascript. My Javascript experience is very minimal, and therefore I'm really stuck on how to create multiple levels within my game. With JSON, I load blocks for Mario to walk on:

createBlocks: function () {
        console.log('game -> createBlocks');

        $.getJSON('js/boxes.json', function (data) {

            data.boxes.level1.forEach(function (blockData) {

                this.stage.removeChild(this.block.el);

                var block = new Block(blockData);
                this.block.push(block);
                this.stage.addChild(block.el);

            }.bind(this));
        }.bind(this));
    }

With the function "createStars" the game loads another JSON. My goal is to have the game switch to another level with every 5 stars being collected. What is the best way to create this with JSON?

My JSON file for the blocks are created as follow:

{
"boxes": {
    "level1": [
        {
            "x": 0,
            "y": 115,
            "width": 25,
            "height": 25
        },
        {
            "x": 25,
            "y": 115,
            "width": 25,
            "height": 25
        }
    ],
    "level2": [
        {
            "x": 0,
            "y": 95,
            "width": 25,
            "height": 25
        }
    ]
}
}

Please let me know if you need my complete code to answer my question? I can also give a link to the game as it currently is hosted on my own site: http://school.carlavanloon.com/cp/

Furthermore, I'd like the game to stop the time after collecting 20 stars. This will then be the end time for the user of the game.

Many thanks in advance for your reply. And please let me know if I need to give any additional information.

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
Caat
  • 77
  • 1
  • 6

1 Answers1

5

Instead of making your json file around "boxes" you should design the json structure top down, first load game json, which contains level objects - which contain boxes arrays etc. ie. something like this

var json = {
    "game_data": {
    "some_global_settings" : {"game_speed": 30, "theme": "dark"},
    "level1": {
        "boxes": [
         {
            "x": 0,
            "y": 115,
            "width": 25,
            "height": 25
         },
         {
            "x": 25,
            "y": 115,
            "width": 25,
            "height": 25
         }
        ],
        "stars": [
         {
            "x": 0,
            "y": 50,
            "width": 25,
            "height": 25
         },
         {
            "x": 125,
            "y": 120,
            "width": 25,
            "height": 25
         }
        ],
        "player_position" : {"x": 0,"y": 50},
        "victory_condition" : {"stars_required" : 5, "time_limit" : "10min"}
       },
       "level2": {"same structure as above with different data" : 1}

    }
    }

and then make a level builder function which picks a level object and creates everything in it. To reload a new level, check the number of stars remaining, if its 0, call your createLevel(gamelevel) with n+1 for building the next level.

below is pseudo code sample.

Every time user collects a star, you will check if it was the last star required, and if so, increment the level counter and call level builder function which could be something like this

function buildLevel( levelNr ) {
    var gdata = json.game_data["level"+levelNr];
    //check if next level exists in game_data object
    if (!gdata) { 
        finishGame();
        return false;
    }

    //next level data exists, so build whats in it
    //clear level from previous stuff
    clearLevel();//perhaps replace canvas with new one

    createBoxes( gdata.boxes);
    createStars( gdata.stars);
    createPlayer( gdata.player_position);

    //everything is ready, add listeners / timers etc, and start the game
}
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
  • Hi @Rainer, Many thanks for your reply. I've tried to work with your solution, but unfortunately the case is to complicated from time to time. How can I load the JSON file as an external file (gameData function) and read it in the levelBuilder and createLevel function? I've tried several things, but the the problem is that the task given by the tutor is to complicated for the knowledge we have so far. Many thanks for your understanding and reply in advance. – Caat Jan 03 '14 at 14:26
  • well, you already have the skills to solve the problem(what you have already done proves that), all you need now is to make level creation more generic. At the moment your createBlocks() function uses hardcoded links and data. Make it use a data passed as parameter. So you can call it like createBlocks(level1Blocks) and later createBlocks(level2Blocks), and so on. – Rainer Plumer Jan 03 '14 at 14:50
  • Hi @Rainer. Thanks for your help. Based on your suggestions and some extra puzzling and trying, I've managed to create the game. Instead of creating completely separate levels, I've created 1 big level with different heights. Thus, after selecting 5 stars, a door appears. When walking to the door, the screen shifts upward (pretending you're in a new level). – Caat Jan 04 '14 at 10:20