我在重复代码中找不到任何错误
我在两个地方(“A”和“B”)使用几乎相同的代码,但它只适用于“A”,而不适用于“B”。我找不到任何问题,也没有收到任何错误。如果有人可以提供帮助,那就太好了!如果您发现我做错了什么,请编辑我的帖子。谢谢!
function setup() {
createCanvas(400, 500);
}
var clicked = false;
var canClick = true;
var test = 1;
function draw() {
if (canClick == true) {
addEventListener('mousedown', e => {
clicked = true;
})
}
addEventListener('mouseup', e => {
clicked = false;
canClick = true;
})
if (test == 1) {
rect(1, 1, 150, 150)
text("Play",30, 90)
textSize(50)
}
//A
if(clicked == true) {
clicked = false;
canClick = false;
if((mouseX > 1) && (mouseX < 151) && (mouseY > 1) && (mouseY < 151)) {
test = 2;
clear()
canClick = true;
}
}
//rock ,paper, scissors
if (test == 2) {
rect(1, 1, 100, 100)
rect(103, 1, 100, 100)
rect(206, 1, 100, 100)
textSize(28)
text("Rock", 18, 60)
text("Paper", 115, 60)
textSize(23)
text("Scissors", 208, 60)
textStyle(BOLD)
}
//B
if(clicked == true) {
clicked = false;
canClick = false;
if((mouseX > 1) && (mouseX < 101) && (mouseY > 1) && (mouseY < 101)) {
console.log("hi")
}
}
}
I'm using almost the same code in two places ("A" and "B"), but it only works at "A", and not at "B". I can't find any problems, and I'm not getting any errors. If someone could help, that would be great! And please edit my post if you see anything that I did wrong. Thanks!
function setup() {
createCanvas(400, 500);
}
var clicked = false;
var canClick = true;
var test = 1;
function draw() {
if (canClick == true) {
addEventListener('mousedown', e => {
clicked = true;
})
}
addEventListener('mouseup', e => {
clicked = false;
canClick = true;
})
if (test == 1) {
rect(1, 1, 150, 150)
text("Play",30, 90)
textSize(50)
}
//A
if(clicked == true) {
clicked = false;
canClick = false;
if((mouseX > 1) && (mouseX < 151) && (mouseY > 1) && (mouseY < 151)) {
test = 2;
clear()
canClick = true;
}
}
//rock ,paper, scissors
if (test == 2) {
rect(1, 1, 100, 100)
rect(103, 1, 100, 100)
rect(206, 1, 100, 100)
textSize(28)
text("Rock", 18, 60)
text("Paper", 115, 60)
textSize(23)
text("Scissors", 208, 60)
textStyle(BOLD)
}
//B
if(clicked == true) {
clicked = false;
canClick = false;
if((mouseX > 1) && (mouseX < 101) && (mouseY > 1) && (mouseY < 101)) {
console.log("hi")
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 p5.js 中,您可以使用
mousePressed()
和mouseReleased()
回调。创建存储按钮矩形的变量:创建一个测试点是否在矩形内的函数:
使用此函数(
mousePressed
回调)来检测何时单击按钮:In p5.js you can use the
mousePressed()
andmouseReleased()
callbacks. Create variables that store the rectangles of the buttons:Create a function that tests whether a point is inside a rectangle:
Use this function, the
mousePressed
callback, to detect when a button is clicked: