Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
How to pass variable value as param in function

I wasnted to know if it was possible to do something along the lines of:

var test = function(testValue){
    if(testValue == true) doThing1();
    if(testValue == false) doThing2();
}

The context of what this is for, I'm obviously new to JS and am creating a small game similar to an agario-but-simpler and I'm using it for movement. My ideal setup is:

class Player {
            /*
             * @param {int} x - x position
             * @param {int} y - y position
             * @param {int} size - size of object
             * 
             */
            constructor(x, y, size) {
                this.x = x;
                this.y = y;
                this.size = size;
                this.points = 0;
                this.velocity = 1;
                this.color = 'white';
                
            }

            addPoints(amount) {
                points += amount;
            }
 
            changeVelocity(newVelocity) {
                velocity = newVelocity;
            }

            moveLeft = function (canMove) {
                if (canMove) { this.x -= this.velocity; }
            }
            moveRight = function (canMove) {
                if (canMove) { this.x += this.velocity; }
            }
            moveUp = function (canMove) {
                if (canMove) { this.y -= this.velocity; }
            }
            moveDown = function (canMove) {
                if (canMove) { this.y += this.velocity; }
            }
        }

I'm looking to have keyDown 'w' set moveUp to true and then complete the movement per screen refresh where I redraw every screen element. Then when I keyUp 'w' set moveUp to false. I'm looking mostly for if this style of variable-value-parameter-passing is possible.

*EDIT: I'm looking for the keyUp/keyDown to change the variable's value which in turn ALSO directly changes the param passed to the corresponding function.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I don't think you need function parameters, use an object property instead. The keyup/keydown code can call player.enableMove() and player.disableMove().

Also, addPoints() and changeVelocity() need to assign this.xxx.

class Player {
  /*
   * @param {int} x - x position
   * @param {int} y - y position
   * @param {int} size - size of object
   * 
   */
  constructor(x, y, size) {
    this.x = x;
    this.y = y;
    this.size = size;
    this.points = 0;
    this.velocity = 1;
    this.color = 'white';
    this.canMove = true;
  }

  addPoints(amount) {
    this.points += amount;
  }

  changeVelocity(newVelocity) {
    this.velocity = newVelocity;
  }
  
  enableMove() {
    this.canMove = true;
  }
  
  disableMove() {
    this.canMove = false;
  }

  moveLeft = function() {
    if (this.canMove) {
      this.x -= this.velocity;
    }
  }
  moveRight = function() {
    if (this.canMove) {
      this.x += this.velocity;
    }
  }
  moveUp = function() {
    if (this.canMove) {
      this.y -= this.velocity;
    }
  }
  moveDown = function() {
    if (this.canMove) {
      this.y += this.velocity;
    }
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda