如何在单击文本和形状时进行注册?

huangapple go评论89阅读模式
英文:

How can i register when text and shapes has been clicked on?

问题

rect(x, y, 100, 100)
text("点击这里", 50, 50)
英文:
rect(x, y, 100, 100)
text("click here", 50, 50)

Is there a way to use mousePressed() so it registers when these two items have been clicked on?

答案1

得分: 1

这似乎是关于碰撞检测的问题。具体来说,你可能正在寻找点-矩形碰撞检测,以确定鼠标是否在矩形内部。

谷歌是你的朋友,但这里有一个示例:

float rectX;
float rectY;
float rectWidth;
float rectHeight;

void setup() {
  size(300, 300);
  rectX = 50;
  rectY = 100;
  rectWidth = 200;
  rectHeight = 100;
}

void draw() {
  background(64);
  
  if (mouseX > rectX && mouseX < rectX + rectWidth && mouseY > rectY && mouseY < rectY + rectHeight) {
    fill(255, 0, 0);
  } 
  else {
    fill(0, 255, 0);
  }
  
  rect(rectX, rectY, rectWidth, rectHeight);
}

不怕自荐:这里有一个关于Processing中碰撞检测的教程。

英文:

It sounds like you're looking for collision detection. Specifically, you're probably looking for point-rectangle collision detection, to determine whether the mouse is inside a rectangle.

Google is your friend, but here's an example:

float rectX;
float rectY;
float rectWidth;
float rectHeight;

void setup() {
  size(300, 300);
  rectX = 50;
  rectY = 100;
  rectWidth = 200;
  rectHeight = 100;
}

void draw() {
  background(64);
  
  if (mouseX &gt; rectX &amp;&amp; mouseX &lt; rectX + rectWidth &amp;&amp; mouseY &gt; rectY &amp;&amp; mouseY &lt; rectY + rectHeight) {
    fill(255, 0, 0);
  } 
  else {
    fill(0, 255, 0);
  }
  
  rect(rectX, rectY, rectWidth, rectHeight);
}

Shameless self-promotion: here is a tutorial on collision detection in Processing.

huangapple
  • 本文由 发表于 2020年1月7日 02:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定