Trying to make a game in JS with three heroes

Trying to make a game in JS with three heroes
0

#1

hey felas im a student learning web mastering. and some friend from the class said me that i should try to make a game in js and it will help me to understand better the js functions and the whole language :slight_smile:
so i started with creating the class the the 3 heroes and everything was fine till i get stuck in the method of the hit.
the problen that i dont know if i should put this method in the class or in each hero.
and how does the methond need to look like in the return…
i was angry so i deleted the whole project hehe if ill get the answer for this question ill start from begining :slight_smile:


#2

If I understand you correctly, you’re talking about a method called hit() that would be used to deal damage to the hero. Part of the confusion here may lie in the fact that JavaScript doesn’t have any formal class structure, although we can encapsulate data in other ways.

I would do something like this:

function Hero() {
  var health = 100;
  // ... other hero "fields"

  function _hit(amount) {
     health -= amount
   }
   // ... other hero "methods"

   // hero function returns an API
   return { hit: _hit, heal: _heal, sleep: _sleep}
}

Then this function can be called as many times as needed to create heroes

var hero1 = Hero();
var hero2 = Hero();

hero1.hit(20) // health is down to 80
hero2.hit(100) // hero is dead

To be honest, you’re going to need to write more code to get a good answer. Otherwise, how will anyone know what you’re trying to accomplish?