我在重复代码中找不到任何错误

发布于 2025-01-09 19:52:13 字数 1296 浏览 1 评论 0原文

我在两个地方(“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2025-01-16 19:52:13

p5.js 中,您可以使用 mousePressed()mouseReleased() 回调。创建存储按钮矩形的变量:

var button1 = [1, 1, 150, 150]
var button2 = [1, 1, 100, 100]
var button3 = [103, 1, 100, 100]
var button4 = [206, 1, 100, 100]

创建一个测试点是否在矩形内的函数:

function inRectangle(x, y, r) {
    return x > r[0] && x < r[0] + r[2] &&
           y > r[1] && y < r[1] + r[3];
}

使用此函数(mousePressed 回调)来检测何时单击按钮:

function mousePressed() {
    if (test == 1) {
        if (inRectangle(mouseX , mouseY, button1)) {
            test = 2
        }
    } else if (test == 2) {
        if (inRectangle(mouseX , mouseY, button2)) {
            console.log("Rock")
        } else if (inRectangle(mouseX , mouseY, button3)) {
            console.log("Paper")
        }
        else if (inRectangle(mouseX , mouseY, button4)) {
            console.log("Scissors")
        }
    }
}

function setup() {
    createCanvas(400, 500);
}

var test = 1;
var button1 = [1, 1, 150, 150]
var button2 = [1, 1, 100, 100]
var button3 = [103, 1, 100, 100]
var button4 = [206, 1, 100, 100]

function inRectangle(x, y, r) {
    return x > r[0] && x < r[0] + r[2] &&
           y > r[1] && y < r[1] + r[3];
}

function mousePressed() {
    if (test == 1) {
        if (inRectangle(mouseX , mouseY, button1)) {
            test = 2
        }
    } else if (test == 2) {
        if (inRectangle(mouseX , mouseY, button2)) {
            console.log("Rock")
        } else if (inRectangle(mouseX , mouseY, button3)) {
            console.log("Paper")
        }
        else if (inRectangle(mouseX , mouseY, button4)) {
            console.log("Scissors")
        }
    }
}

function draw() {
    background(255);
  
    if (test == 1) {
        rect(...button1)
        text("Play",30, 90)
        textSize(50)
    } else if (test == 2) {
        rect(...button2)
        rect(...button3)
        rect(...button4)
        textSize(28)
        text("Rock", 18, 60)
        text("Paper", 115, 60)
        textSize(23)
        text("Scissors", 208, 60)
        textStyle(BOLD)
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

In p5.js you can use the mousePressed() and mouseReleased() callbacks. Create variables that store the rectangles of the buttons:

var button1 = [1, 1, 150, 150]
var button2 = [1, 1, 100, 100]
var button3 = [103, 1, 100, 100]
var button4 = [206, 1, 100, 100]

Create a function that tests whether a point is inside a rectangle:

function inRectangle(x, y, r) {
    return x > r[0] && x < r[0] + r[2] &&
           y > r[1] && y < r[1] + r[3];
}

Use this function, the mousePressed callback, to detect when a button is clicked:

function mousePressed() {
    if (test == 1) {
        if (inRectangle(mouseX , mouseY, button1)) {
            test = 2
        }
    } else if (test == 2) {
        if (inRectangle(mouseX , mouseY, button2)) {
            console.log("Rock")
        } else if (inRectangle(mouseX , mouseY, button3)) {
            console.log("Paper")
        }
        else if (inRectangle(mouseX , mouseY, button4)) {
            console.log("Scissors")
        }
    }
}

function setup() {
    createCanvas(400, 500);
}

var test = 1;
var button1 = [1, 1, 150, 150]
var button2 = [1, 1, 100, 100]
var button3 = [103, 1, 100, 100]
var button4 = [206, 1, 100, 100]

function inRectangle(x, y, r) {
    return x > r[0] && x < r[0] + r[2] &&
           y > r[1] && y < r[1] + r[3];
}

function mousePressed() {
    if (test == 1) {
        if (inRectangle(mouseX , mouseY, button1)) {
            test = 2
        }
    } else if (test == 2) {
        if (inRectangle(mouseX , mouseY, button2)) {
            console.log("Rock")
        } else if (inRectangle(mouseX , mouseY, button3)) {
            console.log("Paper")
        }
        else if (inRectangle(mouseX , mouseY, button4)) {
            console.log("Scissors")
        }
    }
}

function draw() {
    background(255);
  
    if (test == 1) {
        rect(...button1)
        text("Play",30, 90)
        textSize(50)
    } else if (test == 2) {
        rect(...button2)
        rect(...button3)
        rect(...button4)
        textSize(28)
        text("Rock", 18, 60)
        text("Paper", 115, 60)
        textSize(23)
        text("Scissors", 208, 60)
        textStyle(BOLD)
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文