JavaScript - 带 switch 语句的 For 循环不断循环

Sha*_*aun 2 javascript for-loop switch-statement

我对使用 JavaScript 编写和开发基于文本的 RPG 游戏还比较陌生。下面的代码让我能够逐步了解不同的场景,在这些场景中,你会遇到不同的坏人。

我将 For 循环与 Switch 语句结合使用,并让它先工作,但后来我重构了我的代码,使其更加面向对象和原型化。现在我的 For 循环继续循环并且不退出。我自始至终检查了 [i] 的值,发现它正确地变为 0-4,但随后它从 0 重新启动,我不明白为什么?

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();
Run Code Online (Sandbox Code Playgroud)

Mik*_*nte 5

您的循环显然仍将继续,因为您刚刚结束了每个循环的一个情况i,并且仍将测试 数组中的每个值scenario[i]

如何使用变量b作为处理程序,如果you run into a troll执行了类似的事件,则将其设置b为大于 0 的数字,然后b在再次切换到数组之前检查是否已插入值if (b) break;,其中 ifb的值大于 0,则它将被设置为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() {}
Run Code Online (Sandbox Code Playgroud)

答案 2 这是我们游戏开发人员如何通过使用一系列对象数组、对象类等来制作功能性游戏的一种方法。

我将您的代码重新编写为更易于阅读的内容,希望您从中学到一些东西。:)

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 + ": Welcome 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();
Run Code Online (Sandbox Code Playgroud)