0

I am developing a game in JavaScript, which consists in being able to move on the screen and create walls, by holding the left click button, while moving the cursor around on the canvas. On the other hand, by pressing right click, one bullet is created. The problem appears when one bullet is created, by right clicking, which, also, determines the window.onmousedown() function to take place. I do not want to create walls by right clicking on the screen, because the right click is only supposed to create a bullet, not a wall, as the wall should only be created by the left click.

I've tried to create a variable, called wallCannotBeCreated, which is set to false, by default. In my mind, the window.oncontextmenu() function should take place, when right click event takes place, thus wallCannotBeCreated becomes true, in order that window.onmousedown() function is becoming "unable" to create any new wall, until window.onmouseup() function takes place, where wallCannotBeCreated becomes, again, false. Unfortunatelly, it doesn't seem to work, as, always, window.onmousedown() function occurs before window.oncontextmenu() function.

var wallCannotBeCreated = false;
window.oncontextmenu = function (e)
{
    //alert("right click was pressed");
    // a new bullet must be created
    bullets[++counterBullets] = new Bullet(player.x + player.r / 2, player.y, e.x, e.y);
    wallCannotBeCreated = true;
    console.log(wallCannotBeCreated);
}

window.onmousedown = function()
{
  console.log(wallCannotBeCreated);
  if(wallCannotBeCreated == false)
  {
    //console.log("mouse is being pressed");
    Keys.click = true;
    // one wall must be created
    walls[++wallNumber] = new Wall();
    walls[wallNumber].timerStart();
  }
}
window.onmouseup = function()
{
  //console.log("mouse was released");
  Keys.click = false;
  wallCannotBeCreated = false;
}
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
  • 4
    Just check which button has been pressed in `mousedown`: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button – Andreas Jun 15 '19 at 09:55
  • 1
    You are taking the wrong approach by trying to handle right click with the context menu event handler. Disable context menu altogether (https://stackoverflow.com/questions/381795/how-to-disable-right-click-context-menu-in-javascript) and distinguish between left or right mouse button in the mousedown handler (https://stackoverflow.com/questions/1206203/how-to-distinguish-between-left-and-right-mouse-click-with-jquery) – marekful Jun 15 '19 at 09:55
  • Thank you very much, @Andreas ! This solved my problem: window.onmousedown = function(e) { if(e.button == 0) { //console.log("left click"); //console.log("mouse is being pressed"); Keys.click = true; // one wall must be created walls[++wallNumber] = new Wall(); walls[wallNumber].timerStart(); } else if(e.button == 2) { //console.log("right click"); bullets[++counterBullets] = new Bullet(player.x + player.r / 2, player.y, e.x, e.y); } } – Alexandru-Eugen Toma Jun 15 '19 at 10:14

0 Answers0