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

180
Visualizações
How can I detect a click on the background of a JavaScript window?

I am trying to make a JavaScript game on https://code.org game lab to increase reaction time and accuracy, and I cannot figure out how to detect a click on the background to decrease lives. Here is the code I have so far:

1  var score = 0;
2  var lives = 3;
3  var target1 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
4  target1.setAnimation("Target1");
5  target1.scale = 0.5;
6  target1.setCollider("circle");
8  var target2 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
9  target2.setAnimation("Target2");
10 target2.scale = 0.5;
11 target2.setCollider("circle");
12 
13 function draw() {
14         scoreboard();
15         targetClick();
16         life();
17         drawSprites();
18 }
19 
20 function scoreboard() {
21         background("black");
22         textSize(20);
23         text("Lives: " + lives, 10, 375, 100, 100);
24         text("Score: " + score, 10, 10, 100, 100);
25 }
26 function targetClick() {
27         if (mousePressedOver(target1)) {
28                 target1.x = randomNumber(50, 350);
29                 target1.y = randomNumber(50, 350);
30                 score = score + 1;
31         }
32         if (mousePressedOver(target2)) {
33                 target2.x = randomNumber(50, 350);
34                 target2.y = randomNumber(50, 350);
35                 score = score + 1;
36         }
37 }
38 function life() {
39   if (mousePressedOver(mousePressedOver(background)) {
40     lives = lives - 1;
41   }
42   if (lives <= 0) {
43           target1.destroy();
44           target2.destroy();
45           textSize(50);
46           text("Game Over!", 10, 150);
47   }
48 }

Line 39 is where I need it.

Can anyone help?

Edit: After some testing I realized if I could detect a click, just in general like a mousePressed() I could also use mouseX and mouseY to see if it was in the area of one of the targets and if it wasn't, I could remove a life.

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

0

MousePressedOver works if the mouse is pressed and over the element. So users can hold the right click and just move around the canvas to get points. To avoid that and solve the decreasing lives problem, you can use the combination we often use for click events in Game Lab. mouseWentDown() && mouseIsOver(target1)(&& means "and"). This triggers when the mouse clicked and it is over the target. To decrease lives you can simply add ! before the mouseover() since it's the not operator of the JavaScript. It basically means "trigger when the mouse is clicked, and when it is not over the target." Here's the full code;

var score = 0;
  var lives = 3;
  var target1 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
  target1.setAnimation("Target1");
  target1.scale = 0.5;
  target1.setCollider("circle");
  var target2 = createSprite(randomNumber(50, 350), randomNumber(50, 350));
  target2.setAnimation("Target2");
  target2.scale = 0.5;
  target2.setCollider("circle");
 


 function draw() {
         scoreboard();
         targetClick();
         life();
         drawSprites();
 }
 
 function scoreboard() {
         background("black");
         textSize(20);
         text("Lives: " + lives, 10, 375, 100, 100);
         text("Score: " + score, 10, 10, 100, 100);
 }
 function targetClick() {
  
         if (mouseWentDown() && mouseIsOver(target1)) {
                 target1.x = randomNumber(50, 350);
                 target1.y = randomNumber(50, 350);
                 score = score + 1;
                 
         }
         if (mouseWentDown() && mouseIsOver(target2)) {
                 target2.x = randomNumber(50, 350);
                 target2.y = randomNumber(50, 350);
                 score = score + 1;
                 
         }
 }
 

 function life() {
   if (mouseWentDown()&& !mouseIsOver(target1)&& !mouseIsOver(target2)) {
     lives = lives - 1;
   }
   if (lives <= 0) {
           target1.destroy();
           target2.destroy();
           textSize(50);
           text("Game Over!", 10, 150);
   }
 }
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