Javascript 编辑数组中的变量
我正在尝试创建一个“选择你自己的冒险”类型的游戏,目前我正在尝试编写一个“战斗”脚本。到目前为止,我得到的是:Javascript 编辑数组中的变量,javascript,Javascript,我正在尝试创建一个“选择你自己的冒险”类型的游戏,目前我正在尝试编写一个“战斗”脚本。到目前为止,我得到的是: var name = "Anon"; var health = 100; var youAttack = [name + " hits the " + opp + " with his sword", name + " uses magic!", name + " is too scared to fight!"]; var youBattle = function() { v
var name = "Anon";
var health = 100;
var youAttack = [name + " hits the " + opp + " with his sword", name + " uses magic!", name + " is too scared to fight!"];
var youBattle = function() {
var youBattle = youAttack[Math.floor(Math.random() * 3)];
return youBattle;
};
var opp = "Orc";
var oppHealth = 100;
var oppAttack = ["The " + opp + " hits you with his hammer!", "The " + opp + " does nothing!", "The " + opp + " back hands you!"];
var oppBattle = function() {
var oppBattle = oppAttack[Math.floor(Math.random() * 3)];
return oppBattle;
};
oppBattle();
youBattle();
我已经这样做了,所以对手和球员的名字可以很容易地改变
我正在努力弄清楚的是,如何根据使用的攻击增加/减少对手和玩家的生命值。显然,如果opp/玩家什么都不做,生命值不会被移除
有没有一种方法可以在没有一大堆杂乱的if/else语句的情况下实现这一点
我希望得到一些简单的东西,比如name+”用他的剑击“+opp+”+health=health-10代码>但显然这不起作用
提前谢谢
希望这不是太多的代码:
var Attack = function(hero,opp,damageReceived,damageGiven,message){
this.message = message;
this.damageGiven = damageGiven;
this.damageReceived = damageReceived;
this.opp = opp;
this.hero = hero;
this.attack = function(opp){
this.hero.health -= damageReceived;
this.opp.health -= damageGiven;
return this.message;
};
};
var Character = function(name,health){
this.name = name;
this.health = health;
};
hero = new Character('Anon',100);
orc = new Character('Orc',150);
attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");
attacks = [attack1,attack2,attack3,attack4];
while(hero.health > 0 && orc.health > 0){
console.log(attacks[Math.floor(Math.random() * 4)].attack());
console.log('Hero Health: '+ hero.health);
console.log('Orc Health: '+ orc.health);
}
if(hero.health > 0 ){
console.log(hero.name + ' won');
} else {
console.log('The ' + orc.name + ' won');
}
希望这不是太多的代码:
var Attack = function(hero,opp,damageReceived,damageGiven,message){
this.message = message;
this.damageGiven = damageGiven;
this.damageReceived = damageReceived;
this.opp = opp;
this.hero = hero;
this.attack = function(opp){
this.hero.health -= damageReceived;
this.opp.health -= damageGiven;
return this.message;
};
};
var Character = function(name,health){
this.name = name;
this.health = health;
};
hero = new Character('Anon',100);
orc = new Character('Orc',150);
attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");
attacks = [attack1,attack2,attack3,attack4];
while(hero.health > 0 && orc.health > 0){
console.log(attacks[Math.floor(Math.random() * 4)].attack());
console.log('Hero Health: '+ hero.health);
console.log('Orc Health: '+ orc.health);
}
if(hero.health > 0 ){
console.log(hero.name + ' won');
} else {
console.log('The ' + orc.name + ' won');
}
希望这不是太多的代码:
var Attack = function(hero,opp,damageReceived,damageGiven,message){
this.message = message;
this.damageGiven = damageGiven;
this.damageReceived = damageReceived;
this.opp = opp;
this.hero = hero;
this.attack = function(opp){
this.hero.health -= damageReceived;
this.opp.health -= damageGiven;
return this.message;
};
};
var Character = function(name,health){
this.name = name;
this.health = health;
};
hero = new Character('Anon',100);
orc = new Character('Orc',150);
attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");
attacks = [attack1,attack2,attack3,attack4];
while(hero.health > 0 && orc.health > 0){
console.log(attacks[Math.floor(Math.random() * 4)].attack());
console.log('Hero Health: '+ hero.health);
console.log('Orc Health: '+ orc.health);
}
if(hero.health > 0 ){
console.log(hero.name + ' won');
} else {
console.log('The ' + orc.name + ' won');
}
希望这不是太多的代码:
var Attack = function(hero,opp,damageReceived,damageGiven,message){
this.message = message;
this.damageGiven = damageGiven;
this.damageReceived = damageReceived;
this.opp = opp;
this.hero = hero;
this.attack = function(opp){
this.hero.health -= damageReceived;
this.opp.health -= damageGiven;
return this.message;
};
};
var Character = function(name,health){
this.name = name;
this.health = health;
};
hero = new Character('Anon',100);
orc = new Character('Orc',150);
attack1 = new Attack(hero,orc,5,0,"The " + orc.name + " back hands you!");
attack2 = new Attack(hero,orc,0,0,hero.name + " is too scared to fight!");
attack3 = new Attack(hero,orc,15,0,"The " + orc.name + " hits you with his hammer!");
attack4 = new Attack(hero,orc,0,25,hero.name + " uses magic!");
attacks = [attack1,attack2,attack3,attack4];
while(hero.health > 0 && orc.health > 0){
console.log(attacks[Math.floor(Math.random() * 4)].attack());
console.log('Hero Health: '+ hero.health);
console.log('Orc Health: '+ orc.health);
}
if(hero.health > 0 ){
console.log(hero.name + ' won');
} else {
console.log('The ' + orc.name + ' won');
}
我可以直接告诉您,无论您使用哪种语言,尝试编写这种类型的代码都会使用大量if/else和更多语句。您可以使用数组保存攻击模式的值:
var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]
function youAttack(ATK, PHit) {
if(playerHit) {
playerDamage = ATK + PHit;
oppHealth = oppHealth - playerDamage;
return oppHeath;
} else {
alert("You missed!");
}
}
但是,没有看到你在做什么,我不能说你应该如何进行攻击和伤害。我只能假设。您需要一个评估攻击、未命中等的系统,至少在某些地方使用IF/ELSE语句。我可以直接告诉您,无论您使用何种语言,尝试编写此类代码都会使用大量IF/ELSE语句和更多语句。您可以使用数组保存攻击模式的值:
var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]
function youAttack(ATK, PHit) {
if(playerHit) {
playerDamage = ATK + PHit;
oppHealth = oppHealth - playerDamage;
return oppHeath;
} else {
alert("You missed!");
}
}
但是,没有看到你在做什么,我不能说你应该如何进行攻击和伤害。我只能假设。您需要一个评估攻击、未命中等的系统,至少在某些地方使用IF/ELSE语句。我可以直接告诉您,无论您使用何种语言,尝试编写此类代码都会使用大量IF/ELSE语句和更多语句。您可以使用数组保存攻击模式的值:
var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]
function youAttack(ATK, PHit) {
if(playerHit) {
playerDamage = ATK + PHit;
oppHealth = oppHealth - playerDamage;
return oppHeath;
} else {
alert("You missed!");
}
}
但是,没有看到你在做什么,我不能说你应该如何进行攻击和伤害。我只能假设。您需要一个评估攻击、未命中等的系统,至少在某些地方使用IF/ELSE语句。我可以直接告诉您,无论您使用何种语言,尝试编写此类代码都会使用大量IF/ELSE语句和更多语句。您可以使用数组保存攻击模式的值:
var attackName = ["Punch", "Sword", "Magic"]
var attackDamage = [3, 5, 4]
function youAttack(ATK, PHit) {
if(playerHit) {
playerDamage = ATK + PHit;
oppHealth = oppHealth - playerDamage;
return oppHeath;
} else {
alert("You missed!");
}
}
但是,没有看到你在做什么,我不能说你应该如何进行攻击和伤害。我只能假设。您需要一个评估攻击、未命中等的系统,该系统至少在某个地方使用IF/ELSE语句。那么您想在特定条件下添加或删除该值吗?是的。假设兽人用锤子打你,你会失去10点生命值。但如果他还给你,你只会损失5分。如何将其添加到数组中,或者使用最少的编码?在运行时不会对youAttack
变量求值,只要声明它,它就会用常量初始化。在运行时构建字符串,或者(脏的)在其周围加上引号并使用eval()
。对不起@Jongware,我对JavaScript还是相当陌生,我不能说我真的理解你认为我应该做什么。eval()
不是用于数学方程式吗?我不知道如何合并它。基本上,你有类似a=5;b=a+1;a=10
并且您希望b
更改为11
。JavaScript不是这样工作的eval
不限于数学,它可以“评估”(即“运行”)任何Javascript。在文档中的某个地方查找它。因此,您想在特定条件下添加或删除该值吗?是的。假设兽人用锤子打你,你会失去10点生命值。但如果他还给你,你只会损失5分。如何将其添加到数组中,或者使用最少的编码?在运行时不会对youAttack
变量求值,只要声明它,它就会用常量初始化。在运行时构建字符串,或者(脏的)在其周围加上引号并使用eval()
。对不起@Jongware,我对JavaScript还是相当陌生,我不能说我真的理解你认为我应该做什么。eval()
不是用于数学方程式吗?我不知道如何合并它。基本上,你有类似a=5;b=a+1;a=10
并且您希望b
更改为11
。JavaScript不是这样工作的eval
不限于数学,它可以“评估”(即“运行”)任何Javascript。在文档中的某个地方查找它。因此,您想在特定条件下添加或删除该值吗?是的。假设兽人用锤子打你,你会失去10点生命值。但如果他还给你,你只会损失5分。如何将其添加到数组中,或者使用最少的编码?在运行时不会对youAttack
变量求值,只要声明它,它就会用常量初始化。在运行时构建字符串,或者(脏的)在其周围加上引号并使用eval()
。对不起@Jongware,我对JavaScript还是相当陌生,我不能说我真的理解你认为我应该做什么。eval()
不是用于数学方程式吗?我不知道如何合并它。基本上,你有类似a=5;b=a+1;a=10
并且您希望b
更改为11
。JavaScript不是这样工作的eval
不限于数学,它可以“评估”(即“运行”)任何Javascript。在文档中的某个地方查找它。因此,您想在特定条件下添加或删除该值吗?是的。假设兽人用锤子打你,你会失去10点生命值。但如果他还给你,你只会损失5分。如何将其添加到数组中,或者使用最少的编码?在运行时不会对youAttack
变量求值,只要声明它,它就会用常量初始化。比伊