我对使用 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 = …Run Code Online (Sandbox Code Playgroud)