我试图创造一个基本的文字冒险游戏,似乎已经陷入了对我的角色应用伤害。我可以得到损坏的数字,但由于某些原因,似乎没有影响他们的健康,有人有什么建议吗?
function Person(firstName, lastName, hp, concious, str, def, agi, frc ) {
this.firstName = firstName;
this.lastName = lastName;
this.hp = hp;
this.concious = concious;
this.str = str;
this.def = def;
this.agi = agi;
this.frc = frc;
var sayName = function(p) {
console.log(p.firstName);
}
this.attack = function(Person) {
var attPow = Math.floor(Math.random() * this.str);
var attSpe = Math.floor(Math.random() * this.agi);
var defAtt = Math.floor(Math.random() * Person.def);
var defAgi = Math.floor(Math.random() * Person.agi);
console.log(attPow, attSpe, defAtt, defAgi);
if (attPow > defAtt && attSpe > defAgi){
return Person.hp -= attPow + attSpe - defAtt;
console.log(Person.hp);
} if(attPow < defAtt && attSpe < defAgi) {
alert("You missed");
} if (attPow < defAtt) {
alert("He blocked you're attack");
} if (attSpe < defAgi) {
alert("He dodged you're attack");
}
}
};
var Angelo = new Person("Angelo", " ", 75, 50, 5, 5, 5, 5);
var boy = new Person("boy","dead", 75,50,5,2,5,5);
Angelo.attack(boy);发布于 2016-07-28 00:48:45
正如“天使的回答”所指出的。对于您的场景,您应该使用if关键字将所有的else-if语句打包到一个块中。
您的代码很好,它可以工作,但只在间歇性的基础上工作。原因是因为您在if块中遗漏了一个不适合您的特定情况。当您在条件检查中添加一个else时,您会注意到这一点。
if (attPow > defAtt && attSpe > defAgi){
console.log("ATTACK!");
return Person.hp -= attPow + attSpe - defAtt;
} else if(attPow < defAtt && attSpe < defAgi) {
console.log("You missed");
} else if (attPow < defAtt) {
console.log("He blocked you're attack");
} else if (attSpe < defAgi) {
console.log("He dodged you're attack");
}
else {
console.log("????");
}
var Angelo = new Person("Angelo", " ", 75, 50, 5, 5, 5, 5);
var boy = new Person("boy","dead", 75,50,5,2,5,5);
console.log("before HP:" + boy.hp);
Angelo.attack(boy);
console.log("after HP:" + boy.hp);在男孩的HP未被触及的情况下。你会看到这个;
输出:
惠普之前:75 0 1 0 1 ? 惠普后:75
注意到console.log("????")是如何触发的吗?这意味着你错过了一个你没有处理的场景。建议您为attPow、defAtt、attSpe和defAgi变量转储else子句的值,并查看所缺少的内容。
发布于 2016-07-28 00:41:31
如果不详细说明,您的代码不能工作的一个明显原因是代码的以下部分是错误的:
if (attPow > defAtt && attSpe > defAgi){
return Person.hp -= attPow + attSpe - defAtt;
console.log(Person.hp);
} if(attPow < defAtt && attSpe < defAgi) {
alert("You missed");
} if (attPow < defAtt) {
alert("He blocked you're attack");
} if (attSpe < defAgi) {
alert("He dodged you're attack");
}要创建完整的检查,必须以以下格式链接这些if语句:
if (attPow > defAtt && attSpe > defAgi){
return Person.hp -= attPow + attSpe - defAtt;
console.log(Person.hp);
} else if(attPow < defAtt && attSpe < defAgi) {
alert("You missed");
} else if (attPow < defAtt) {
alert("He blocked you're attack");
} else if (attSpe < defAgi) {
alert("He dodged you're attack");
}https://stackoverflow.com/questions/38625229
复制相似问题