How to handle addeventlistener event click in javascript

Asked

Viewed 2,244 times

0

I need to develop a game using HTML 5 and Javascript with the game inside a canvas.

I created a game opening screen and after that click on play it directs to another screen. I called this way the event click

document.getElementById('canvas').addEventListener('click', verificaClick, false);

Where canvas is the id of my canvas and verificaClick the function that sends to the next step according to the selected option because I have the options "play", "how to play" and "credits".

If I click play it directs to a page where I choose the "stage" or "stage" of the game I want to play, more or less like Candy Crush.

My problem is that the canvas redesigns everything right, however remains the click event of the first part of the game, the first drawing of the canvas. I am redesigning, erasing the context and redesigning, but it remains the same click on the positions of the first "buttons" I drew.

Is correct the section where I call the click event?

document.getElementById('canvas').addEventListener('click', verificaClick, false);

If not, how should I do it; Put multiple contexts and add the event in context? How would I do that?

  • I edited your question using code formatting instead of bold (in the editor, the button {} does so). But I don’t quite understand the problem. You want to simply remove Event Listener?

1 answer

1


One possible solution is to remove the Istener after use, and add another referring to the next state. This can be done within your checkClick function, after you validate that the click was actually on the button to the next screen.

function verificaClick(){
    // ...

    // Não sei exatamente como você faz a validação, mas vamos supor
    // que a condição abaixo verifique o clique sobre o botão 'Jogar':
    if(x > 100 && x < 300 && y > 350 && y < 410){
        // ...
        document.getElementById('canvas').removeEventListener('click', verificaClick, false);
        document.getElementById('canvas').addEventListener('click', verificaClickLevelSelect, false);
        // ...
    }

    // ...
}

function verificaClickLevelSelect(){
    // ...

    // Caso seja detectado um clique no botão 'Voltar', por exemplo:
    if(x > 50 && x < 150 && y > 50 && y < 90){
        // ...
        document.getElementById('canvas').removeEventListener('click', verificaClickLevelSelect, false);
        document.getElementById('canvas').addEventListener('click', verificaClick, false);
        // ...
    }

    // ...
}

Of course, your navigation tree can be HUGE. In this case, this method can be too rudimentary and difficult to maintain. A more elaborate implementation could include "automatic" maintenance of listeners through an object in the global scope, which would store the state machine and be updated by a helper method to each navigation.

I won’t take your pleasure in programming that machine, it sounds fun! :)

  • 1

    Thanks ai saw face, it worked, I had completely forgotten how to use the Istener, I forgot that I could remove, thanks even

  • Ah, whine, it was nothing :) That’s what we’re there for!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.