【发布时间】:2020-06-22 13:57:17
【问题描述】:
我目前正在尝试找到一种方法,以允许对象中的函数在对象被调用后运行。
比如下面这个Object...
const textNodes = [
{
id: 1,
faction: "Greaser",
text: "Do you want to help this person?",
options: [
{
text: "Yes",
nextText: 2
},
{
text: "No",
nextText: 2
},
]
},
{
id: 2,
faction: "greaser",
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,
logic: function update() {
console.log("update function worked")
if(character.greaser == true && this.faction == "greaser"){
console.log("greaser check 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);
},
},
]
}
]
如您所见,我有一个函数update(),一旦调用该函数,它将更改一个字符对象。
我目前正在调用该函数,例如... textNodes[1].options[1].logic();
当然,我不想这样做,因为我正在构建一个 rpg 决策游戏。用户将获得一个问题和一些选择,并允许创建自己的路径。如果用户得到 3 个选项,并且用户选择了选项 2,我将运行选项 2 的 logic() / update() 函数。
每个游戏选项都会附加一个功能,它会做不同的事情,每次我选择选项时,我都希望该功能自动运行。我不确定这是否可能,但这似乎是最合乎逻辑的选择。
我正在构建的纸牌决策游戏类似于https://www.youtube.com/watch?v=ByBUsBxPrZ4。
正如您在此视频中看到的,每张选择的卡片(火种应用风格)都会更新游戏和角色统计数据。
我希望这些更新函数在调用对象选项后立即运行。我不确定这是否可能,一位同行告诉我my hint to you is to use classes,但我不经常使用类,除非我用 java 编码,坦率地说我不喜欢。
抱歉,如果这很难理解并提前致谢。
这是我尝试过的。
const textNodes = [
{
id: 1,
faction: "Greaser",
text: "Do you want to help this person?",
options: [
{
text: "Yes",
nextText: 2
},
{
text: "No",
nextText: 2
},
]
},
{
id: 2,
faction: "greaser",
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,
logic: function update(){
console.log("update function worked")
if(character.greaser == true && this.faction == "greaser"){
console.log("greaser check 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);
},
run2: () =>{
this.logic();
}
},
]
}
]
【问题讨论】:
-
我觉得你可以帮忙proxy
-
您能否详细说明“一旦调用对象选项”。 “叫”是什么意思?据我了解,您希望避免显式调用逻辑函数并自动调用它。我只是不明白触发器。
-
你知道用户选择了哪个选项吗?如果是,您是否尝试直接在所选选项上调用
logic方法?您甚至可以检查对象是否具有像这样定义的logic属性:jsbin.com/nuyenagiwe/1/edit?js,console -
@ΣπύροςΓούλας 感谢您的评论。当我的意思是“被调用”时,我的意思是每当用户为该问题 ID 选择一个“选项”时,我都想运行我的对象中的所有函数。因此,如果问题(id 2)是“你想跑还是打”,并且用户选择“打”选项,则该选项中的任何功能都将运行。如果用户选择战斗,也许我的函数会从玩家对象中减去生命值。我以本教程为起点,可能有助于理解我想要做什么。 youtube.com/watch?v=R1S_NhKkvGA&t=709s.
标签: javascript function oop