Skip to content
Advertisement

How can I keep all objects updated in this OOP example?

I don’t know exactly how to explain it, but I’ll try my best.

Here is a simple OOP example where you create players to kill a dragon. The point is to assign to each player a specific amount of damage to inflict on a dragon, in order to kill it.

For instance, if a dragon has 200 health and 2 players are chosen to attack it, the players will be assigned to deal 100 damage each.

Here’s the structure of the OOP:

JavaScript

Here are manipulations I’m testing:

JavaScript

This prints the following:

JavaScript

The issue is that, since Josh is the first player to be tasked with killing Gamacial and Nibiru, he’s assigned to deal the full damage on these dragons, despite the fact that Steven and Robert are also tasked with killing that dragon.(Same issue with the dragon Nibiru).

In other words, it’ll always be the last player that’ll have the actual correct damage to deal, while the others will always be one step behind the calculation because they’ve been assigned the task before taking into count the next player.

It should therefore print:

JavaScript

Is there any way I keep ALL players in sync with each other? So after Steven gets assigned the same dragon as Josh, Josh gets updated, etc.?

Advertisement

Answer

maybe you should calculate later, after all player registered to dragons:

JavaScript

register:

JavaScript

then calculate:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement