'How to edit values of a specific instance of a class with another specific instance of a class within a vector in javascript
i am currently attempting to code a little game in Javascript, it is not going well. i am currently stuck at the start, i have only just been able to work out how to get the class working properly. the class i am talking about is cards, they are then stored in the deck vector and later on they will be stored in the hand vector once drawn, they have stuff such as heath / dmg / name / cost. i want these cards to be able to effect each other in combat, ie a card with 2 dmg should be able to reduce the heath of a card with 2 heath to 0 and then it dies. while i am sure i will have many more issues currently the main one is getting two cards to effect each other, within a vector, without changing its position. plus i need to be able to have a different range of cards attach a range of different other cards when confronted, thus why they can not change position / i can not just create a new instance of it.
this card game is like mtg in that it has costs to play the cards, collected from resource and refunded resources from already played cards once they are destroyed.
this is my current code, there is not much.
const instances = [];
class Cards {
constructor(name, cost, health, dmg){
this.name = name;
this.cost = cost;
this.health = health;
this.dmg = dmg;
this.displayCard = () =>{
console.log(`${name} ${cost} ${health} ${dmg}`);
};
instances.push(this);
}
static Getcards() {
instances.forEach( x => {
x.displayCard();
});
}
}
new Cards('Fisherman', 2, 8, 2);
new Cards('Cat', 5,2,7);
new Cards('dog', 11,3,1);
instances[1].displayCard();
Cards.Getcards();
I Have attempted to google and stack overflow this already but i cant seem to find what i need or even work out how i might go about it. i am very stuck and google is not helping too much. i could do with some pointing in the right direction, i havent got too long to sort this out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
