[英]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 中的代碼
如果你想在 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.