简体   繁体   English

如何在处理中使用鼠标单击?

[英]How to use mouse click in Processing?

I was working on an animation on processing.我正在处理 animation 。 Then, I have a question about the lights.然后,我有一个关于灯的问题。 Normally, my code is more long.通常,我的代码更长。 However, I made a simple code which can usefull also for the beginners.但是,我制作了一个简单的代码,对初学者也很有用。

boolean isOn = false;             // Variable keeping the state of the light
float sphereSize = 100;           // The size of Shpere
float xS = 200; yS = 200; zS = 0; // The coordinates of Sphere

void setup()
{
  size(400, 400, P3D);
  noStroke();
}

void draw()
{
  background(0);
  if (isOn)                       // Checks the state in which the light should be
    lights();
  translate(xS, yS, zS);          // Translate the sphere to the middle of window
  sphere(sphereSize);             // Making a sphere for see ligts
}

void mouseReleased() {            // This function is automatically called in draw method
  if (isOn)                       // After a click the state of the light is inverted
      isOn = false;
    else isOn = true;
}

So, I want to work light only if the mouse click on the sphere.所以,我只想在鼠标点击球体时轻装上阵。 If the mouse click outside the sphere, it will not work.如果鼠标在球体外单击,它将不起作用。 How to solve this problem?如何解决这个问题呢? Thanks.谢谢。

Use dist() to compute the Euclidean distance between the mouse position ( mouseX , mouseY ) and the center of the sphere ( xS , yS ).使用dist()计算鼠标yS ( mouseX , mouseY ) 和球心 ( xS , mouseY ) 之间的欧几里得距离
If the distance is less or equal than the radius ( sphereSize ), then switch the light:如果距离小于或等于半径( sphereSize ),则切换灯光:

void mouseReleased() {

    float distance = dist(xS, yS, mouseX, mouseY);
    if (distance <= sphereSize)
        isOn = !isOn;
}

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

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