Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

286
Vistas
Cómo pasar el valor de la variable como parámetro en la función

Quería saber si era posible hacer algo como:

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

El contexto de para qué es esto, obviamente soy nuevo en JS y estoy creando un pequeño juego similar a un agario pero más simple y lo estoy usando para el movimiento. Mi configuración ideal es:

 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; } } }

Estoy buscando tener keyDown 'w' establecer moveUp en verdadero y luego completar el movimiento por actualización de pantalla donde vuelvo a dibujar cada elemento de la pantalla. Luego, cuando keyUp 'w' configuro moveUp en false . Estoy buscando principalmente si este estilo de paso de parámetro de valor variable es posible.

* EDITAR: estoy buscando keyUp/keyDown para cambiar el valor de la variable que, a su vez, TAMBIÉN cambia directamente el parámetro pasado a la función correspondiente.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

No creo que necesite parámetros de función, use una propiedad de objeto en su lugar. El código keyup/keydown puede llamar a player.enableMove() y player.disableMove() .

Además, addPoints() y changeVelocity() deben asignar 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda