JavaScript - 用于循环使用switch语句不断循环

时间:2014-07-24 01:33:53

标签: javascript for-loop switch-statement

我是一个相对较新的编码和使用JavaScript的基于文本的RPG游戏。以下代码允许我逐步介绍与不同的坏人接触的不同场景。

我将一个For循环与一个Switch语句结合使用并让它先工作,但后来我重新考虑了我的代码,使它更加OO&原型。现在我的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();

1 个答案:

答案 0 :(得分:3)

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

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

答案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();