最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - For loop with switch statement continually looping - Stack Overflow

programmeradmin1浏览0评论

I'm relatively new to coding and working on a text based RPG game in JavaScript. The following code allows me to progress through the different scenarios where you are approached with a different bad guy.

I used a For loop in conjunction with a Switch statement and had it working prior, but then I re factored my code to make it more OO & prototypal. Now my For loop continues looping and does not exit. I checked the value of [i] throughout and see it properly goes 0-4, but then it restarts at 0 and I can't figure out why?

var scenario = new Array();

 //simple function to create the number of scenarios
 function Scenario () {
    howManyScenarios = function(number) {
       for (i=0; i <= number; i++) {
       scenario[i] = ("Scenario " + (1 + i));
    };
};

howManyScenarios(4); //if you change the argument, add additional switch cases

//iterating through my howManyScenarios function to build out scenarios using a switch case
createScenarios = function () {
    var ii = scenario.length;

    for (i=0; i < ii; i++) {

    switch(scenario[i]) {
        case 'Scenario 1':
            alert("You run into a troll");
            b = 0;
            break;
        case 'Scenario 2':
            alert("You find a store to purchase goods from");
            ItemShop();
            break;
        case 'Scenario 3': 
            alert("You run into a ogre");
            b = 1;
            break;
        case 'Scenario 4':
            alert("You run into a warewolf");
            b = 2;
            break;
        case 'Scenario 5':
            alert("You run into a wizard");
            b = 3;
            return;
            break;  
        }; //close out switch cases
    }; //close out my for loop
}; //close out createScenarios function 
createScenarios();
}; //close out Scenario function

Scenario();

I'm relatively new to coding and working on a text based RPG game in JavaScript. The following code allows me to progress through the different scenarios where you are approached with a different bad guy.

I used a For loop in conjunction with a Switch statement and had it working prior, but then I re factored my code to make it more OO & prototypal. Now my For loop continues looping and does not exit. I checked the value of [i] throughout and see it properly goes 0-4, but then it restarts at 0 and I can't figure out why?

var scenario = new Array();

 //simple function to create the number of scenarios
 function Scenario () {
    howManyScenarios = function(number) {
       for (i=0; i <= number; i++) {
       scenario[i] = ("Scenario " + (1 + i));
    };
};

howManyScenarios(4); //if you change the argument, add additional switch cases

//iterating through my howManyScenarios function to build out scenarios using a switch case
createScenarios = function () {
    var ii = scenario.length;

    for (i=0; i < ii; i++) {

    switch(scenario[i]) {
        case 'Scenario 1':
            alert("You run into a troll");
            b = 0;
            break;
        case 'Scenario 2':
            alert("You find a store to purchase goods from");
            ItemShop();
            break;
        case 'Scenario 3': 
            alert("You run into a ogre");
            b = 1;
            break;
        case 'Scenario 4':
            alert("You run into a warewolf");
            b = 2;
            break;
        case 'Scenario 5':
            alert("You run into a wizard");
            b = 3;
            return;
            break;  
        }; //close out switch cases
    }; //close out my for loop
}; //close out createScenarios function 
createScenarios();
}; //close out Scenario function

Scenario();
Share edited Jul 24, 2014 at 2:13 jww 103k103 gold badges445 silver badges946 bronze badges asked Jul 24, 2014 at 1:33 ShaunShaun 231 silver badge3 bronze badges 3
  • Sorry, can't reproduce: jsfiddle/R552r – Ian Commented Jul 24, 2014 at 1:40
  • 2 I can't see how these bits of code fit together. I will say that you're using a global instance of the variable i and that might be causing you trouble. Try declaring i within each function with var i; and see if that helps. – user1864610 Commented Jul 24, 2014 at 1:43
  • "howManyScenarios" creates an array [Scenario 1, Scenario 2, etc] which I then iterate through in "createScenarios". The reason I'm doing it that way is because I don't know how many scenarios I might decide to use, so I don't want to build them all out separately. Tried to declare each var i and that didn't work. It does properly iterate through each scenario, but then i resets to 0 for some reason and it starts over at Troll – Shaun Commented Jul 24, 2014 at 11:15
Add a ment  | 

1 Answer 1

Reset to default 5

Your loop will obviously still continue because you just only ended a case of every loop of i and will still test each value in the array of scenario[i].

How about using variable b as a handler that if an event like you run into a troll has been executed, then set b to a number greater than 0 then check if a value has been inserted into b before switching into the array again using if (b) break; where if b has a value greater than 0 then it will be set as true.

var scenario = new Array();
var b;
//simple function to create the number of scenarios
function Scenario() {
    howManyScenarios = function (number) {
        for (i = 0; i <= number; i++) {
            scenario[i] = ("Scenario " + (1 + i));
        };
    };

    howManyScenarios(4); //if you change the argument, add additional switch cases
    console.log(scenario[i]);
    //iterating through my howManyScenarios function to build out scenarios using a switch case
    createScenarios = function () {
        var ii = scenario.length;

        for (i = 0; i < ii; i++) {
            if (b) break;
            switch (scenario[i]) {
                case 'Scenario 1':
                    alert("You run into a troll");
                    b = 1;
                    break;
                case 'Scenario 2':
                    alert("You find a store to purchase goods from");
                    b = 2;
                    ItemShop();
                    break;
                case 'Scenario 3':
                    alert("You run into a ogre");
                    b = 3;
                    break;
                case 'Scenario 4':
                    alert("You run into a warewolf");
                    b = 4;
                    break;
                case 'Scenario 5':
                    alert("You run into a wizard");
                    b = 5;
                    return;
                    break;
            }; //close out switch cases
        }; //close out my for loop
    }; //close out createScenarios function 
    createScenarios();
}; //close out Scenario function

Scenario();

function ItemShop() {}

ANSWER 2 This one is one way on how we game developers make a functional game by using a series of object arrays, object classes and the like.

I remade your code into something easier to read, hope you learn something from this. :)

var numberofscenarios = 5;
var scenario = []; //array where scenarios will be
//this will be the accessible properties of scenario[] array
var _scenario = function(){
    this.name = ""; //name of scenario
    this.message =  "";
    this.doSomething = 0;
    this.status = 0 ;//1 = finished and 0 = false
};
var _event = function(mobname){
    this.mobname = mobname;
    this.battle = function(){//doSomething
                            console.log("Battle VS "+ this.mobname +" Start!");
                            };
    this.itemShop =  function(){//doSomething
        console.log(this.mobname + ": Wele to the shop! How may I help you?");
                };
};

//generate the scenarios in the scenario[] array
function generateScenarios() {
    for (i = 0; i <= numberofscenarios; i++) {
            scenario[i] = new _scenario();
            scenario[i].name = i;

        switch (scenario[i].name) {
            case 1:
                scenario[i].message = "You run into a Troll";
                scenario[i].doSomething = new _event("Troll");      
                break;
            case 2:
                scenario[i].message = "You find a store to purchase goods from";
                scenario[i].doSomething = new _event("Shop Keeper");
            break;
            case 3:
                scenario[i].message = "You run into a Ogre";
                scenario[i].doSomething = new _event("Ogre");
                break;
            case 4:
                scenario[i].message = "You run into a Werewolf";
                scenario[i].doSomething = new _event("Werewolf");
                break;              
            case 5:
                scenario[i].message = "You run into a Wizard";
                scenario[i].doSomething = new _event("Wizard");
                break;
        }
    }
}
generateScenarios(); //generate the scenarios

//test the array of scenario class

//test the battle with Troll
console.log(scenario[1].message);
scenario[1].doSomething.battle();

//test the shop
console.log(scenario[2].message);
scenario[2].doSomething.itemShop();

//attempt to fight the Shopkeeper
console.log(scenario[2].message);
scenario[2].doSomething.battle();
发布评论

评论列表(0)

  1. 暂无评论