0

I was trying to change the property for the objects inside this._keys. The method I am using worked fine to change the property but the problem is I have an if statement that decides if the property gets changed or not and that if statement checks if a variable is equal to a string. That variable is dynamic so I have to setInterval to keep on updating the data. But when I setInterval I cant update the property of the object anymore. I get the error Cannot set properties of undefined (setting 'forward'). Why can't I change the property while updating the data? I have commented on my code to better point out the problem. Any help on why this occurs and how to fix it is appreciated. Thanks in advance.

const currentKeylogW, currentKeylogA,
  currentKeylogD, currentKeylogS = a dynamic data;


class BasicCharacterControllerInput2 {
  constructor() {
    this._Init();
  }
  
  
  _Init() {

    this._keys = {
      forward: false,
      backward: false,
      left: false,
      right: false,
      space: false,
      shift: false,
      shiftForward: false,
    };

    //this has no error but currentKeylog's variables are not being updtes so its useless
    if (currentKeylogW == '"w"') {
      this._keys.forward = true;
    } else if (currentKeylogA == '"a"') {
      this._keys.left = true;
    } else if (currentKeylogD == '"d"') {
      this._keys.left = true;
    } else if (currentKeylogS == '"s"') {
      this._keys.right = true;
    } else if (currentKeylogW == '"!w"') {
      this._keys.forward = false;
    } else if (currentKeylogA == '"!a"') {
      this._keys.left = false;
    } else if (currentKeylogD == '"!d"') {
      this._keys.left = false;
    } else if (currentKeylogS == '"!s"') {
      this._keys.right = false;
    }


    //updates currentKeylog's variables but can't set forward,.. properties
    setInterval(function() {

      if (currentKeylogW == '"w"') {
        this._keys.forward = true;
      } else if (currentKeylogA == '"a"') {
        this._keys.left = true;
      } else if (currentKeylogD == '"d"') {
        this._keys.left = true;
      } else if (currentKeylogS == '"s"') {
        this._keys.right = true;
      } else if (currentKeylogW == '"!w"') {
        this._keys.forward = false;
      } else if (currentKeylogA == '"!a"') {
        this._keys.left = false;
      } else if (currentKeylogD == '"!d"') {
        this._keys.left = false;
      } else if (currentKeylogS == '"!s"') {
        this._keys.right = false;
      }

    }, 500)

  }
}
seriously
  • 1,202
  • 5
  • 23

0 Answers0