Processing 将鼠标拖动到可单击区域之外
我试图从头开始编写一个滑块,我有一个基本的功能,但我希望能够将滑块拖动到我为单击它而创建的边界之外Processing 将鼠标拖动到可单击区域之外,processing,Processing,我试图从头开始编写一个滑块,我有一个基本的功能,但我希望能够将滑块拖动到我为单击它而创建的边界之外 void slide() { if ((x+w >= mouseX) && (mouseX >= x) && (y+h >= mouseY) && (mouseY >= y) && (mousePressed == true)) { h = (mouseY - y); }
void slide()
{
if ((x+w >= mouseX) && (mouseX >= x) && (y+h >= mouseY) && (mouseY >= y) &&
(mousePressed == true))
{
h = (mouseY - y);
}
如您所见,当鼠标拖动到滑块尺寸之外时,高度值不再更改。如何仅在尺寸内激活,然后拖动到尺寸外?我已通过布尔值来控制单击和锁定,如:
class Test {
//global
float x;
float y;
float w, h;
float initialY;
boolean lock = false;
//constructors
//default
Test () {
}
Test (float _x, float _y, float _w, float _h) {
x=_x;
y=_y;
initialY = y;
w=_w;
h=_h;
}
void run() {
float lowerY = height - h - initialY;
float value = map(y, initialY, lowerY, 100, 255);
color c = color(value);
fill(c);
rect(x, initialY, 4, lowerY);
fill(200);
rect(x, y, w, h);
float my = constrain(mouseY, initialY, height - h - initialY );
if (lock) y = my;
}
boolean isOver()
{
return (x+w >= mouseX) && (mouseX >= x) && (y+h >= mouseY) && (mouseY >= y);
}
}
//end of class
Test[] instances = new Test[3];
void setup() {
size(200, 600);
noStroke();
instances[0] = new Test(20, 20, 40, 20);
instances[1] = new Test(80, 20, 40, 20);
instances[2] = new Test(140, 20, 40, 20);
}
void draw() {
background(100);
for (Test t:instances)
t.run();
}
void mousePressed() {
for (Test t:instances)
{
if (t.isOver())
t.lock = true;
}
}
void mouseReleased() {
for (Test t:instances)
{
t.lock = false;
}
}
看起来会很好,我需要自己定义mousePressed()吗?尽管它被突出显示,但似乎不起作用。感谢lotNo,mousePressed()是一个内置方法。它应该有用。。。出什么问题了?我在run中的slider类中使用了它,在draw()下调用了它。我到底在哪里/如何使用它?(我发布了一些测试代码作为对我的问题的编辑,但它需要被审查)你帮了大忙,谢谢。啊,现在我看到了你的编辑,它已经被一些人拒绝了。但是你应该在任何其他的块外使用鼠标垫和鼠标垫。不是在你班里。此外,您还需要检查rect的内部性,否则它将在任何地方都像鼠标点击一样工作。我会和你的同学再次修改代码,等一下…很高兴它能工作。请注意:在代码注释中,我说过需要一个全局布尔值。。。嗯,不是真的,事实上可能不是你想要的。。。您可以锁定一个成员变量并对其进行设置,每个实例都没有鼠标保护,比如
instance.lock=true代码>。如果您有多个滑块,这可能很重要。。。