繁体   English   中英

如果(mousePressed)不工作(Processing.js)

[英]If(mousePressed) is not working (Processing.js)

当用户点击它时,我想打开一扇门。 代码如下所示:

with(processing) {
        noStroke();
        size(1200, 900);
        background(255, 153, 153);
        var x = 50;
        var y = 50;
        if(mousePressed && (mouseButton === LEFT)) {
            fill(139, 69, 19);
            rect(x+150, y, 150, 200); // door opened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+(335-50), y+100, 10, 10); // handle
        }
        else {
            fill(139, 69, 19);
            rect(x, y, 150, 200); // door unopened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+15, y+100, 10, 10); // handle
        }
    };

但是,当我点击门时,它不会打开。 当我的鼠标被按下时,它似乎甚至不明白。 我想知道我的代码有什么问题。 我尝试将相同的代码复制粘贴到可汗学院(除了我使用 mouseIsPressed 而不是 mousePressed)并且它有效吗? 那么为什么它在我的 IDE 中不起作用? 我使用 VSCode 顺便说一句。

谢谢!

您需要将代码移动到mousePressed事件处理程序

mousePressed = function() {
    noStroke();

        background(255, 153, 153);
        var x = 50;
        var y = 50;
        if(mousePressed && (mouseButton === LEFT)) {
            fill(139, 69, 19);
            rect(x+150, y, 150, 200); // door opened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+(335-50), y+100, 10, 10); // handle
        }
        else {
            fill(139, 69, 19);
            rect(x, y, 150, 200); // door unopened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+15, y+100, 10, 10); // handle
        }
};

Khanacdemy 中的代码

https://www.khanacademy.org/computer-programming/spin-off-of-mousepressed-processingjs/5395110149373952

如果你想在 HTML 文件中运行,你需要在type="application/processing"的脚本标签中包含脚本代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script src="processing.js"></script>
  <script type="application/processing">
    /* @pjs preload="images/grass.jpg"; */

// When mouse is clicked, grow the flower
void mouseClicked(){
    alert(1)
    draw();
  };

void draw(){

        background(255, 153, 153);
        var x = 50;
        var y = 50;
        if(mousePressed && (mouseButton === LEFT)) {
            fill(139, 69, 19);
            rect(x+150, y, 150, 200); // door opened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+(335-50), y+100, 10, 10); // handle
        }
        else {
            fill(139, 69, 19);
            rect(x, y, 150, 200); // door unopened
            fill(89, 60, 31);
            rect(x, y, 150, 200); // doorway
            fill(255, 222, 173);
            ellipse(x+15, y+100, 10, 10); // handle
        }
};
  </script>

</body>
</html>

暂无
暂无

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

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