首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >损坏似乎不适用于物体?

损坏似乎不适用于物体?
EN

Stack Overflow用户
提问于 2016-07-28 00:22:50
回答 2查看 49关注 0票数 0

我试图创造一个基本的文字冒险游戏,似乎已经陷入了对我的角色应用伤害。我可以得到损坏的数字,但由于某些原因,似乎没有影响他们的健康,有人有什么建议吗?

代码语言:javascript
运行
复制
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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-28 00:48:45

正如“天使的回答”所指出的。对于您的场景,您应该使用if关键字将所有的else-if语句打包到一个块中。

您的代码很好,它可以工作,但只在间歇性的基础上工作。原因是因为您在if块中遗漏了一个不适合您的特定情况。当您在条件检查中添加一个else时,您会注意到这一点。

代码语言:javascript
运行
复制
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("????")是如何触发的吗?这意味着你错过了一个你没有处理的场景。建议您为attPowdefAttattSpedefAgi变量转储else子句的值,并查看所缺少的内容。

票数 0
EN

Stack Overflow用户

发布于 2016-07-28 00:41:31

如果不详细说明,您的代码不能工作的一个明显原因是代码的以下部分是错误的:

代码语言:javascript
运行
复制
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语句:

代码语言:javascript
运行
复制
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");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38625229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档