简体   繁体   English

如何在 p5.js 中启用右键单击?

[英]How to enable right click in p5.js?

I'm making a minesweeper clone, left click reveals the cell and right click is supposed to flag the cell.我正在制作扫雷艇克隆,左键单击显示单元格,右键单击应该标记单元格。

I'm using the function mouseClicked() - this allows me to reveal on left click and also reveal on right click.我正在使用功能mouseClicked() - 这允许我在左键单击时显示并在右键单击时显示。

I tried using我尝试使用

if(mouseIsPressed) {
    if(mouseButton === LEFT) {
        reveal
    }
    if(mouseButton === RIGHT) {
        flag
    }
}

This registers once every frame that the button is held down.这将在按下按钮的每一帧中注册一次。 I just want a single right click.我只想单击右键。 I imagine there is something simple that I'm missing, but I really can't find anything in the documentation or anyone asking about it online.我想我遗漏了一些简单的东西,但我真的在文档中找不到任何东西,也没有任何人在线询问它。

TL;DR - I just want to be able to right click in p5.js. TL; DR - 我只想能够在 p5.js 中右键单击。

The mouseClicked() event only occurs once when the mouse button is pressed. mouseClicked()事件仅在按下鼠标按钮时发生一次。

Set a state flag when the event occurs:事件发生时设置状态标志:

var rightPressed = false;

function mouseClicked() {
    if(mouseButton === RIGHT) {
        rightPressed = true;
    }
}

Reset the flag when the action which is triggered by the event was handled:处理事件触发的操作时重置标志:

function draw() {

    if (rightPressed) {
        rightPressed = false

        // do somthing
        // ...
    } 
}

Use the mouseClicked() function and pass the event .使用mouseClicked()函数并传递event

Then using event.button you can determine if it is a right click like so:然后使用event.button您可以确定它是否是右键单击,如下所示:

function mouseClicked(event){
  if(event.button === 2){ 
    // Right click 
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM