javascript - Adding multiple levels in game created with JSON - Stack Overflow

For one of my school projects, I have to create a game with Javascript. My Javascript experience is ver

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 plete code to answer my question? I can also give a link to the game as it currently is hosted on my own site: /

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.

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 plete 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./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.

Share Improve this question edited Jan 2, 2014 at 13:58 Guillermo Gutiérrez 17.9k18 gold badges92 silver badges118 bronze badges asked Jan 2, 2014 at 13:35 CaatCaat 771 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745675217a4639645.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信