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