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

197
Vistas
la función no se llama en los gráficos de JavaScript

Estaba haciendo un juego de tres en raya en gráficos de JavaScript (creo) y llamé a una función para obtener la fila del clic (la función que no funciona es la función getRow) y no hay ningún resultado de la función

 var WIDTH = 400; var HEIGHT = 400; setSize(WIDTH, HEIGHT); var WINNING_LINE_WIDTH = 10; var WINNING_LINE_COLOR = Color.red; //high var width = getWidth() / 3; //thick var height = getHeight() / 3; //x != o var shape = 0; //1 2 3, as easy as ABC var row = 0; //ready, set, GO!!!! function start(){ grid(); mouseClickMethod(click); } //lines function grid(){ var line1 = new Line(width, 0, width, getHeight()); var line2 = new Line(width * 2, 0, width * 2, getHeight()); var line3 = new Line(0, height, getWidth(), height); var line4 = new Line(0, height * 2, getWidth(), height * 2); add(line1); add(line2); add(line3); add(line4); } //notrick function click(e){ var clicky = e.getY(); getRow(clicky,row); println(row); println(clicky); println(height); println(height * 2); println(height * 3); } //row row row ur boat function getRow(clicky, row){ if(clicky < height){ return row + 1; } if(clicky >= height && clicky <= height * 2 ){ return row + 2; } else{ return row + 3; } return row - 1; println("nothing"); }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Este código funciona correctamente para lo que escribió, pero hay algunas cosas que querrá cambiar para obtener la funcionalidad deseada.

En primer lugar, los valores de retorno de las funciones se devuelven a través de la propia llamada de matriz. Parece que en su código de ejemplo está pasando "clicky" y "fila" como argumentos, y tratando de establecer el valor de esas variables en la función. Sin embargo, cuando crea parámetros, en realidad está sobrescribiendo cualquier otra variable en el alcance actual con el mismo nombre y creando variables locales en su lugar, y simplemente está pasando el valor de las variables en su llamada de función. Así que esto es lo que está sucediendo en su programa:

 var row = 0; function getRow(row) { // defining row as a parameter creates a new local variable called "row" row = 1; // sets the "row" local variable to 1, but doesn't affect the "row" outside of the function console.log(row); // prints "1" because that's the value of the local var } getRow(row); console.log(row); // prints 0 because that's the value of the global variable "row"

Entonces, lo que querrá hacer es pasar el valor y de su cursor al hacer clic (¡lo que ya está haciendo! Lo guarda como el parámetro de función "clicky") pero no necesita pasar en la fila también. Luego, ¡simplemente puede devolver un número entero para el valor de su fila! (En lugar de agregarlo a la variable global como lo está ahora) Entonces, todo lo que tiene que hacer es establecer la fila en ese valor, configurando la fila igual a la llamada a la función.

 // variable values for example var row = -1; var height = 100 / 3; // each row will be ~33 pixels function click(e){ var clicky = e.getY(); row = getRow(clicky); // set row to the value returned from getRow()! } //row row row ur boat function getRow(clicky){ if(clicky < height){ return 1; } if(clicky >= height && clicky <= height * 2 ){ return 2; } else{ return 3; } return -1; println("nothing"); // also note: you can delete this as it will never run, as its after a return (which stops running the function) } // testing: console.log(row); // before click (-1) click({getY:()=>0}); // test object I made so it can get a y-value (it'll just be your click event in your code) console.log(row); // after clicking at y = 0 (0) click({getY:()=>40}); console.log(row); // after clicking at y = 40 (1) click({getY:()=>90}); console.log(row); // after clicking at y = 90 (2)

¡Espero que esto haya ayudado! Si necesita aclaración sobre cualquier cosa, hágamelo saber.

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