【问题标题】:JavaScript virtual petJavaScript 虚拟宠物
【发布时间】:2015-05-04 21:57:17
【问题描述】:

我正在尝试创建一个基于文本的虚拟宠物护理游戏。我希望能够为您提供两个宠物(具有属性的对象)和函数,以通过修改对象属性与这些对象进行交互。所以这就是我所拥有的:

function Pet(pet_name){
    this.pet_name = pet_name;
    this.pet_hunger = Math.floor((Math.random() * 10) + 1);
    this.pet_health = Math.floor((Math.random() * 10) + 1);
    this.pet_happiness = Math.floor((Math.random() * 10) + 1);

    this.feed = feed;
    this.show = show;
}

pet1 = new Pet("Brian");
pet2 = new Pet("Lassy");

function feed(){
    var amount = Math.floor((Math.random() *2) + 1);
    this.pet_hunger = this.pet_hunger - amount;
    if (this.pet_hunger < 0){
        this.pet_hunger = 0;
    }
    this.show();
 }

function show(){
    var the_string = "";
    if (this.pet_health === 0){
        the_string = this.pet_name + " is dead!";
    }
    else {
        the_string += "Name: " + this.pet_name;
        the_string += "Hunger: " + this.pet_name;
        the_string += "Health: " + this.pet_health;
        the_string += "Happiness: " + this.pet_happinesss;
    }
}

当我运行代码时:

console.log(pet1);
console.log(pet1.feed());  
console.log(pet1);

我收到以下信息:

{ pet_name: 'Brian',
  pet_hunger: 4,
  pet_health: 4,
  pet_happiness: 10,
  feed: [Function: feed],
  show: [Function: show] }
undefined
{ pet_name: 'Brian',
  pet_hunger: 2,
  pet_health: 4,
  pet_happiness: 10,
  feed: [Function: feed],
  show: [Function: show] }

所以我们可以看到feed 函数正在工作。但是,我仍然不确定为什么未定义的节目。现在,我创建了一个名为show 的函数。这应该显示四个人的统计数据(姓名、饥饿、健康、幸福)。但是,当我尝试运行时:

console.log(pet1.show);
console.log(pet1.feed());
console.log(pet1);

我收到以下信息:

[Function: show]
undefined
{ pet_name: 'Brian',
  pet_hunger: 4,
  pet_health: 1,
  pet_happiness: 9,
  feed: [Function: feed],
  show: [Function: show] }

我不确定为什么我的show 函数不起作用。我真的只想让我的控制台干净地显示: 姓名: 饥饿: 健康: 幸福: 有什么想法吗?

【问题讨论】:

  • 您不应编辑问题并将其内容替换为新问题。每个帖子限制自己一个问题,然后通过选择“最佳答案”来结束问题。 Read the StackOverflow guide. 在 JavaScript 中,您必须始终放置括号才能执行函数。将.show 替换为.show();
  • @JacqueGoupil 谢谢,我是 StackOverflow 的新手。我一定会结束这个问题。选择最佳答案后会自动关闭吗?
  • @Nappstir 问题不会“关闭”,但基本上标记为“这解决了我遇到的问题”。根据技术问题的性质,正确/最佳解决方案可能会随着时间的推移而变化,并且将来可能会支持/接受替代(或新)答案

标签: javascript function object properties constructor


【解决方案1】:

例如,feed 方法可以是:

function feed(amountOfFood){
  this.hunger += amountOfFood; //Same as this.hunger = this.hunger + amountOfFood;
}

然后,您可能会有一种方法让用户调用此方法,但我不确定您如何计划用户与游戏交互,因此无法解释更多...

【讨论】:

    【解决方案2】:

    对于初学者,您可能想要跟踪每个属性的最小值(可能为 0)、最大值和当前值。从那里你将只是操纵当前值,例如

    function Pet(name){
        this.name = name;
    
        this.maxHealth = Math.floor((Math.random() * 10) + 1);
        this.currentHealth = this.maxHealth;
    
        this.maxHappiness = Math.floor((Math.random() * 10) + 1);
        this.currentHappiness = this.maxHappiness;
    
        this.maxHunger = Math.floor((Math.random() * 5) + 1);
        this.currentHunger = 0;
    
        this.feed = function(amount = 1) {
            if (amount > this.currentHunger) {
                this.currentHunger = 0;
            } else {
                this.currentHunger -= amount;
            }
        };
    }
    

    【讨论】:

      【解决方案3】:

      JavaScript 具有基于原型的继承。当您执行pet1.something 之类的操作时,它会查看pet1 实例以查看它是否具有名为something 的属性。如果没有,它会查看对象的原型,看看它是否有一个名为something 的属性。如果没有,它会检查原型的原型,依此类推。

      就像在基于类的对象编程中一样,为对象提供方法的聪明方法是为原型提供方法。

      Pet.prototype.feed = function() {
        this.nutrition += 20;
      }; //note the semicolumn
      

      如果你改为在构造函数中声明函数,每个宠物都会有自己的每个函数的副本,这会无缘无故地占用内存。在某些特殊情况下它可能有用,但在您的情况下,请坚持原型方式。

      也许更好的feed 函数可以确保您不会过度喂养宠物。

      Pet.prototype.feed = function() {
          this.nutrition += 20;
          if(this.nutrition > 100) {
             this.health -= 10;
             this.nutrition = 100;
             this.say("I ate too much :S");
          }
      };
      

      您可能希望存储不同统计数据的最大值和最小值。例如,如果任何值变为 0,宠物就会死亡,但如果宠物的能量低于 10,它会睡着,如果它的饥饿或情绪低于 10,它的健康就会受到影响,依此类推。反之亦然,吃得太多的宠物可能会生病。

      那些告诉什么值是重要的常量可以保存在构造函数本身中。您可能希望在每个子对象中存储多个值,如果该值低于该常数,那么您的宠物可能会要求您提供食物、睡眠等。

      Pet.maxAny = 100;
      Pet.hungerCaps = {good: 80, ok: 50, need: 30, bad: 20};
      Pet.sleepCaps = {good: 50, ok: 40, need: 30, bad: 20};
      Pet.moodCaps = {good: 80, ok: 60, need: 40, bad: 30};
      

      然后你可以创建一个随着时间的推移降低所有统计数据的函数。我会让你考虑一下这个问题:)

      【讨论】:

      • @Nappstir 我不确定我是否明白你在问什么,但你不应该让 lassy 成为一个空白对象。我真的不能为您做更多的事情,但请尝试阅读 Mozilla 开发者网络上的课程。
      • NVM 最后评论。我做了一些调整,并能够让我的feed 函数工作。但是,如果您看上面,我似乎找不到show 的好方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多