繁体   English   中英

当调用 object 时,在 object 中自动运行 function

[英]Automatically running a function inside an object when that object is called

我正在使用 Javascript 构建基于文本的选择 RPG 游戏。 我可以在 object 中添加函数并运行它们,但是,如果选择了选项(文本节点),我希望 function 运行。

目前,我声明了一个function,然后在页面底部调用它。 问题是,一旦选择了选项,我希望 function 运行。

这是我到目前为止的代码......

 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();

我尝试了一些不同的东西,包括添加另一个键run: update()run: this.update()run: this.function等。

我也有点困惑,因为我认为 ES6 允许我删除function关键字,但这似乎是不允许的(我可能只是做错了,或者根据我设置 object 的方式遇到问题)。

我不确定如何通过调用textNodes[1].options[1].function来动态调用 go 来动态调用嵌套在特定选项中的 function。

在此先感谢您的帮助。

您可以将此部分更改为:

update: (function(){
        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);
    });

function关键字已更改为update ,然后 function 已更改为匿名 function ......您应该避免使用用户定义的关键字。 然后匿名 function 将不会执行,直到你调用它:

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

这是我正在谈论的一个示例:这表明您必须实际访问 function 和任何其他密钥才能访问这些值。

 var text = [ { data1: "here", data2: (function(){ return "You have to explicitly call me." }) } ] console:log("Without calling any data. " + text[0]) console:log("When you call data2. " + text[0].data2()) console:log("Let us access data1. " + text[0].data1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM