无法访问对象功能内的对象键?

时间:2020-06-20 15:37:23

标签: javascript object javascript-objects

我正在构建基于Web应用程序文本的RPG游戏。从问题中选择每个答案后,游戏将使用textNodes并移至下一个“问题”。

在我的游戏中,我将允许用户在游戏开始时选择特权。

其中一个好处是“加脂”。

此游戏中有多个派系,我想检查玩家是否有加脂振作,并检查当前询问的问题是否包含派系“加脂者”。

如果这两种情况都成立,我将执行相同的逻辑,但损失减少。如果没有,我将按显示的功能运行它。

这是两个问题的代码,其中一个具有功能。

function addRadiatedStatus() {
  character.radiated = true;
}

const textNodes = [
  {
    id: 1,
    text: "Do you want to help this person?",
    options: [
      {
        text: "Yes",
        nextText: 2,
      },
      {
        text: "No",
        nextText: 2,
      },
    ],
    faction: "Greaser",
  },
  {
    id: 2,
    text: "It was a trap, do you run or kill them?",
    options: [
      {
        text: "Run",
        nextText: 3,
        damage: 0,
      },
      {
        text: "Kill",
        nextText: 5,
        damage: 8,
        moneyDrop: 20,
        radiationDamage: 2,
        function: function update() {
          if (character.greaser == true && textNodes[1].faction == "greaser") {
            console.log("greaser check worked");
          }
          console.log("upadte function worked");
          character.health -= this.damage;
          character.cash += this.moneyDrop;
          character.radiationLevel += this.radiationDamage;
          character.maxHP -= this.radiationDamage;
          if (this.radiationDamage >= 1) {
            addRadiatedStatus();
          }
          console.log("character HP" + " " + character.health);
          console.log("character maxHP" + " " + character.maxHP);
          console.log("moneyDrop " + " " + this.moneyDrop);
          console.log("character cash" + " " + character.cash);
          console.log("radiation level" + " " + character.radiationLevel);
          console.log("character Raditated?" + " " + character.radiated);
        },
      },
    ],
    faction: "Greaser",
  },
];

textNodes[1].options[1].function();

如您所见,我正在对textNodes[1].faction进行硬编码,一旦我有数百个问题/选择,它将变得非常烦人。

我正在寻找一种表达类似'textNodes [textNodes.id] .faction'之类的方式,从而使我不必重用代码,而不必进行硬编码。

对于如何自动在问题选择/答案中自动调用任何函数,我也感到困惑,因为目前,我正在像这样的textNodes[1].options[1].function()那样对页面底部的功能调用进行硬编码。

我希望能够做两件事...

  1. 只要玩家选择了选项,就会自动调用该功能

  2. 动态地将textNode ID添加到if / else检查中是否添加了“润滑脂”。

我是Java语言的新手,我不确定为什么我无法从函数中获取键“ id”。

谢谢。

2 个答案:

答案 0 :(得分:1)

更改您的回调函数声明。您正在使用:

function: function update(){

并使用它来调用它:

textNodes[1].options[1].function();

相反,您可以这样写:

update: () => { 

并这样称呼它:

textNodes[1].options[1].update();

此外,如果您不想硬编码任何内容,则可以更改字符对象,而不是布尔值charactergreaser您可以创建字符串characterfaction具有值greaser,那么您可以简单地做到这一点

if (character.faction == this.faction) {
      console.log("greaser check worked");
}

您还可以尝试将id作为update()函数的参数传递。

update: (id) => { // pass id in update function

if (character.faction == textNodes[id].faction) { // check is done
      console.log("greaser check worked");
}

textNodes[1].options[1].update(1); //  this could help in case you want to also check values of other textNodes

答案 1 :(得分:0)

添加另一个功能以查找所需的textNode。然后将该文本节点的函数调用为

function findTextNode(){
   if(character.greaser == true){
   var textNode=textNodes.filter(textNode => textNode.faction === "Greaser") 
   if(textNode){ 
     return textNode
   }
   return null;
}
var textNode=findTextNode();
if(textNode && textNode.update){// if textNode not null and have update function 
  textNode.update();
}

//Then update function will be as

update: function update(){
... function body goes here ...
   }