0

I'm stuck with the following:

attack: function() {
var p = roundSystem.instancePlayer();
var e = roundSystem.instanceCurrentEnemy();

e.eH -= p.pAD;
console.log(e.eH);

Is there a way to call the function with the inside functions only called once ?

I'm trying to do a Attack button where the enemy gets damaged. with the call I'm also creating copies of each the enemy and player, so I can later reuse the root object values for reseting the game. Problem is, this way calling the attack will result in no life reduction for the enemy, because the copies get called too and thus reseting their values.

Edit: Better explanation

e.eH stands for the enemy life
p.pAD stands for the player damage

Now once the attack button is clicked, the enemy gets damaged by the amount of the player Attack damage. The purpose of the copies from the objects are to make a reset function later on if one of the two dies. I can use the values from the copied objects than hardcoding, for instance, every case of health from the enemies.

Thanks in advance!

Source code

  • 1
    I'm not entirely sure I understand what your issue is, but it sounds like your functions are doing too much. Functions should ideally only do a single, simple thing. Why not make copies outside of the function? – Carcigenicate May 26 '17 at 20:41
  • I thought that would be bad behaviour because they are being global variables ? I wanted to make special functions for those to keep them in their own scope. – codeForBreakFast May 26 '17 at 20:43
  • Maybe you can try to use a static variable. https://stackoverflow.com/questions/1535631/static-variables-in-javascript – Unkall May 26 '17 at 20:45
  • @codeForBreakFast Copy them in a other function, then have that function call `attack`. Or have a function that calls 2 functions: 1 that copies, and another that handles attacking. By "outside of the function", I meant outside of `attack`, not globally outside of all functions. – Carcigenicate May 26 '17 at 20:45
  • I'm not sure I understand, either, but can't you just check whether you've already made a copy of the player, and not call the function in that case? – Barmar May 26 '17 at 21:25
  • @Carcigenicate Can you show an example of how you mean it ? I don't quite get it I made the function but they haven't been called, except in the attack function which is called when the player presses the attack button. If nothing works I will just use global variables to get the job done – codeForBreakFast May 26 '17 at 21:31
  • @codeForBreakFast I'm honestly having problems understanding your issue, and a lot of code is missing, so it would be difficult for me to produce anything. I don't get why you're making copies in the first place though. – Carcigenicate May 26 '17 at 21:41
  • Don't call `instancePlayer()` and `instanceCurrentEnemy()` from the `attack` function at all. Put them outside. – Bergi May 26 '17 at 22:50
  • @Bergi but how do I access it from outside ? It's erroring me out of scope. – codeForBreakFast May 26 '17 at 23:12
  • @codeForBreakFast Show us that code, please. – Bergi May 27 '17 at 16:34

0 Answers0