Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Necesito ayuda para escribir código específico usando javascript

Quiero escribir código algo como esto:

 if ( [*distance between two objects*] = [*for example: 20px*] ) { [*run someFunction()*] }

He escrito muchos códigos y lógicas diferentes, pero es un poco diferente para mí...

Hice un mini juego usando js como un juego de dino cromado... varios obstáculos se acercan al corredor que debe evitar, puedes saltar usando el botón de espacio pero ahora quiero hacer que el salto automático funcione cuando un obstáculo se acerque a él y habrá una distancia especifica entre ellos quiero que se active la funcion de salto...

tal vez tengo que usar el teorema de Pitágoras, algo como esto?

 var a = x1 - x2; var b = y1 - y2; var c = Math.sqrt( a*a + b*b ); // c is the distance if(c == "20px" ) { onJump(); };

Pero, ¿cómo puedo usarlo prácticamente?

Por favor ayúdame si puedes...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Para obtener la distancia que desea hacer algo como esto.

Solo tenga en cuenta que estoy usando el bloque de visualización en línea, agrega un pequeño margen adicional entre los elementos, por lo tanto, el bit adicional de px.

 window.onload = function() { var eleY = document.getElementById('y'), eleX = document.getElementById('x'), y = eleY.getBoundingClientRect().left, x = eleX.getBoundingClientRect().right, distance = y - x; eleY.innerHTML = 'y = ' + y; eleX.innerHTML = 'x = ' + x; document.getElementById('distance').innerHTML = 'distance = ' + distance; if(distance == 138){ alert('JUMP'); } }
 div { padding:35px 0; width: 80px; display: inline-block; text-align:center; vertical-align:middle; } #x { background: red; } #distance { width:100px; padding:10px 5px; color:#fff; background:green; margin:0 10px; } #y { background: yellow; }
 <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript"> </script> </head> <body> <div id="x"></div> <div id="distance"></div> <div id="y"></div> </body> </html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!