我被告知制作一个游戏将是学习如何正确使用javascript的好方法所以我已经开始了一个游戏,到目前为止玩家被提示创建一个英雄,然后根据其英雄为其分配属性类型(magerouge,necromancer,术士或萨满)然而,当我到达分配属性的点时,它总是说用户选择了一个死灵法师,无论他实际选择了什么类。所以简言之,我的函数叫做“defaultAssign”。我希望我正确地发布这个问题,如果我发布错误请告诉我,所以我可以尝试修复它,这是我的第一个问题。这是我的代码:
var heroArray = [];
var yourHero ="";
var hero = {
characterType:"",
Damage:0,
Health:0,
Mana:0,
ManaRegenRate:0,
HealthRegenRate:0,
SpecialSkills:[]
};
var mainMenu = function(){
var nameCheck = prompt("What is your Character's Name?").toUpperCase() ;
for( var i = 0;i <= heroArray.length ; i++){
if (nameCheck === heroArray[i]){
alert("We have found your hero change this string later");
runGame(heroArray[i]);
}
else{
alert("You Must Create a Champion");
var heroName = prompt("What Will You Name Your Sorcerer!").toUpperCase;
characterCreator(heroName);
/*use a loop with a regular expression here to check if the name is avalible, if it is countinue, if not
prompt the user for another name
*/
}
/* run "gameSave" for particular hero
Run the main Game function and print to the console:
"Ah yes "+yourHerosNameHere+"," +hisOrHer+" tale echoes far and wide. We last spoke of his journey to"
+insertCurrentCityHere+" where "+heOrShe+" "mostRecentAction"."
*/
}
}
var characterCreator = function(yourHero){
yourHero = Object.create(hero);
yourHero.characterType = prompt("Choose your Character Type:\n"+
"MageRouge\n"+
"Warlock\n"+
"Shaman\n"+
"Necromancer").toUpperCase;
defaultAssign(yourHero.characterType)
}
function defaultAssign(playersType){
for (var j = 0 ; j <= 3 ; j++){
if (playersType === "MAGEROUGE"){
yourHero.Damage=25;
yourHero.Health=50;
yourHero.Mana=15;
yourHero.ManaRegenRate=1;
yourHero.HealthRegenRate=0.4;
yourHero.SpecialSkills=[["pickpocket",],["sneak",],["lockpick",]];
alert("Ahha a powerful Magerouge, choose your skills emphasis wisely,"
+" it could determine your Destiny...");
skillAssigner(yourHero);
break;
}
if(playersType === "WARLOCK"){
yourHero.Damage=50;
yourHero.Health=50;
yourHero.Mana=25;
yourHero.ManaRegenRate=0.6;
yourHero.HealthRegenRate=0.3;
yourHero.SpecialSkills=[["summonDemon",0],["bindDemon",0],["portal",0],["illusion",0]];
alert("Ahha a powerful Warlock, choose your skills emphasis wisely,"
+"it could determine your Destiny...");
skillAssigner(yourHero);
break;
}
if(playersType === "SHAMAN"){
yourHero.Damage=40;
yourHero.Health=50;
yourHero.Mana=30;
yourHero.ManaRegenRate=0;
yourHero.HealthRegenRate=0.6;
yourHero.SpecialSkills=[["weatherControl",0],["heal",0],["astralProjection",0]]
alert("Ahha a powerful Shaman choose your skills emphasis wisely,"
+"it could determine your Destiny...");
skillAssigner(yourHero);
break;
}
else if(playersType === "NECROMANCER") {
yourHero.Damage=60;
yourHero.Health=50;
yourHero.Mana=20;
yourHero.ManaRegenRate=0.8;
yourHero.HealthRegenRate=0.4;
yourHero.SpecialSkills=[["raiseDead",0],["petrify",0],["soulSap",0]];
alert("Ahha a powerful Necromancer choose your skills emphasis wisely,"
+"it could determine your Destiny...");
skillAssigner(yourHero);
break;
}
}
}
/*
create an array of hometowns for the main character to choose from
*/
function skillAssigner(yourHero){
for (var s = 0;s<3;s++){
var p = 0;
while( p < 10 ){
var n = prompt("How many points will you spend on "+yourHero.SpecialSkills[s]+"?");
yourHero.SpecialSkills[s][1] = n;
p +=n;
}
}
}
mainMenu();
答案 0 :(得分:2)
摆脱检查玩家类型是死灵法师的行上的else
。没有必要。
函数for
内部不需要defaultAssign
循环(删除它时,break
也不行。)
最后,toUpperCase
是一个函数,因此您必须使用括号toUpperCase()
调用它。
解决这些问题可以使您的代码正常工作。
您应该设计格式化代码(或获取为您执行此操作的编辑器)以提高其可读性 - 这将有助于您找到错误。