Jump to content

Cannot load new level automatically


sofea
 Share

Recommended Posts

Hai, i am Sofea from Malaysia. 

I am doing my final year project (Real Time Strategy Game) using html but i have some problem with my code. Whenever i completed the first level, the next level wont load. So can anybody help me with the code?

tq ;)

 

var singleplayer = {
    // Begin single player campaign
    start:function(){
        // Hide the starting menu layer
        $('.gamelayer').hide();
        
        // Begin with the first level
        singleplayer.currentLevel= 0;
        game.type = "singleplayer";
        game.team = "blue";
                
        // Finally start the level
        singleplayer.startCurrentLevel();
    },    
    exit:function(){
        // Show the starting menu layer
        $('.gamelayer').hide();
        $('#gamestartscreen').show();
    },
    currentLevel: 0,
    startCurrentLevel:function(){                
        // Load all the items for the level
        var level = maps.singleplayer[singleplayer.currentLevel];

        // Don't allow player to enter mission until all assets for the level are loaded
        $("#entermission").attr("disabled", true);

        // Load all the assets for the level
        game.currentMapImage = loader.loadImage(level.mapImage);
        game.currentLevel = level;

        game.offsetX = level.startX * game.gridSize;
        game.offsetY = level.startY * game.gridSize;

        // Load level Requirements 
        game.resetArrays();
        for (var type in level.requirements){
               var requirementArray = level.requirements[type];
               for (var i=0; i < requirementArray.length; i++) {
                   var name = requirementArray;
                   if (window[type]){
                       window[type].load(name);
                   } else {
                       console.log('Could not load type :',type);
                   }
               };
           }

        for (var i = level.items.length - 1; i >= 0; i--){
            var itemDetails = level.items;
            game.add(itemDetails);
        };        

        // Create a grid that stores all obstructed tiles as 1 and unobstructed as 0
        game.currentMapTerrainGrid = [];
        for (var y=0; y < level.mapGridHeight; y++) {
            game.currentMapTerrainGrid[y] = [];
               for (var x=0; x< level.mapGridWidth; x++) {
                game.currentMapTerrainGrid[y][x] = 0;
            }
        };
        for (var i = level.mapObstructedTerrain.length - 1; i >= 0; i--){            
            var obstruction = level.mapObstructedTerrain;
            game.currentMapTerrainGrid[obstruction[1]][obstruction[0]] = 1;
        };
        game.currentMapPassableGrid = undefined;
    
        // Load Starting Cash For Game
        game.cash = $.extend([],level.cash);
    
        // Enable the enter mission button once all assets are loaded
        if (loader.loaded){
            $("#entermission").removeAttr("disabled");
            
            
        } else {
            loader.onload = function(){
                $("#entermission").removeAttr("disabled");
            
            }
        }

        // Load the mission screen with the current briefing
        $('#missonbriefing').html(level.briefing.replace(/\n/g,'<br><br>'));    
        $("#missionscreen").show();        
    },
    play:function(){
        fog.initLevel();
        game.animationLoop();                        
        game.animationInterval = setInterval(game.animationLoop,game.animationTimeout);
        game.start();
    },    
    sendCommand:function(uids,details){
        game.processCommand(uids,details);
    },
    endLevel:function(success){
        clearInterval(game.animationInterval);
        game.end();

        if (success){
            var moreLevels = (singleplayer.currentLevel < maps.singleplayer.length-1);
            if (moreLevels){
                //check if there are more levels
                game.showMessageBox("Mission Accomplished.",function(){
                    $('.gamelayer').hide();
                    singleplayer.currentLevel++;
                    singleplayer.startCurrentLevel();
                });
            } else {
                game.showMessageBox("Mission Accomplished.<br><br>This was the last mission in the campaign.<br><br>Thank You for playing.",function(){
                    $('.gamelayer').hide();
                    $('#gamestartscreen').show();
                });
            }
        } else {
            game.showMessageBox("Mission Failed.<br><br>Try again?",function(){
                $('.gamelayer').hide();
                singleplayer.startCurrentLevel();
            }, function(){
                $('.gamelayer').hide();
                $('#gamestartscreen').show();
            });
        }
    }
};

 

 

Black War.zip

Link to comment
Share on other sites

Simplest form of debugging is still throwing in console.logs to ensure that certain code blocks (or lines) are being executed.

For example, try adding one in the `showMessageBox` handler where you increment the `currentLevel` variable, you might want one a little earlier too, perhaps at the start of the `endLevel` function to test that your conditionals work and that the code you _think_ is executing actually *is* executing. You can also log at the start of the `startCurrentLevel` function and check what the current level is.

All of this can also be achieved by setting breakpoints and stepping through execution (check the docs for how to do this for your preferred browser, all of the modern ones have great tooling for doing this). Most browser let you set a breakpoint on a console log, or you can be heavy handed and place `debugger` in your code, which will trigger your editor to use that instruction as a breakpoint. Plain console logs will probably solve this problem for you (its worth learning how to use debugging tools at some point though).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...