While I'm making rpg games with Javascript, I'm trying to add abnormal conditions and enhancements.Therefore, we have added a condition array to each character constructor that represents each character.
However, I am troubled because I cannot get the value of the characters from the array.
Code
function Chara(hp, attack){
This.myname="chara"
This.hp = hp;
this.attack=attack;
This.condition=[];
}
functionCondition(target){
This.myname="target"
this.target=target;
}
Condition.prototype.effect=function(){
This.Target+=50;
}
Condition.prototype=Object.create(Chara);
const Chara1 = new Chara(50,50);
const Condition 1 = new Condition ("attack");
Chara1.condition.push (Condition 1);
Condition 1.effect();
// I want to add 50 to the target.
What should I do if I want this to point to chara1 in this effect function?
javascript
I want this to point to chara1 in this effect function
If this is the only way to do it, you can do it by calling effect
as follows:
Condition1.effect.apply(Chara1);
However, this won't make what the questioner really wants to do.
Judging from the code, the target
property in Condition
specifies which of the properties Chara
has to be corrected.
If you do not leave the effect
function this
at Condition
, the target
property will become inaccessible and will not achieve its purpose.
There are many possible actions to take, but Chara
is easy to give as an argument for effect
.
Condition.prototype.effect=function(chara){
chara [this.target] + = 50;
}
// Condition.prototype=Object.create(Chara); I don't think I need this
const Chara1 = new Chara(50,50);
const Condition 1 = new Condition ("attack");
Chara1.condition.push (Condition 1);
Condition 1.effect (Chara1);
To understand why chara[this.target]
can be written, use keywords such as JavaScript bracket notation.
Also, I think we should actually try to make the condition
array of Chara
work properly, but it seems to be beyond the scope of this question, so I'll stop here.
© 2024 OneMinuteCode. All rights reserved.