捕获值栏
我正在研究我的大学项目,并遇到了一个问题。我正在创建一个在处理中的滑块,将价值更改为0-100。我想使用我创建的按钮来捕捉10(10,20,30 ...)的值。它的外观是:
我很抱歉,如果我的代码看起来很混乱,我是初学者。 这是我的代码:
int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0;
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0;
float syOffset = 0.0;
void setup() {
size(550,300);
}
void draw() {
background(0);
fill (100);
rect (75, 25, 400, 50); //the slider track
if(mousePressed) {
if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
{x=mouseX;}
}
float value = map(x, 75, 475, 0, 100);
fill(255);
rect (x, 20, 3, 60); //the slider thumb, positioned at x}
fill (100);
if(mousePressed){
if (mouseX > bx-boxSize && mouseX < bx+boxSize &&
mouseY > by-boxSize && mouseY < by+boxSize){
if(x <=75){
x=76;}
fill(255);
if(x >=475){
x= x-40;
}else if (x >= 435){
x = x-40;
}
}
}
rect(bx, by, boxSize, boxSize);
fill(10);
triangle(30, 50, 50, 40, 50, 60);
fill(100);
if(mousePressed){
if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize &&
mouseY > sby-sboxSize && mouseY < sby+sboxSize){
if(x >= 475){
x=474;}
fill(255);
x++;
}
}
rect(483,25,50,50);
fill(10);
triangle(520, 50, 500, 40, 500, 60);
println(value);
line (115, 60, 115, 90);
line (155, 60, 155, 90);
line (195, 60, 195, 90);
line (235, 60, 235, 90);
line (275, 60, 275, 90);
line (315, 60, 315, 90);
line (355, 60, 355, 90);
line (395, 60, 395, 90);
line (435, 60, 435, 90);
}
我开始尝试弄清楚如何使其从100到90到80,依此类推。但是我遇到了一个问题,如果我按下按钮,它将达到80,而不是首先停止到90。如果(x&gt; = 435){x = x-40; 语句,我假设这是由于我的else el el el
。但是我似乎无法找到解决此问题的方法。
I am working on my university project and came across an issue. I am creating a slider in Processing that changes value to 0-100. And I want to it to snap into values of 10 (10,20,30...) using the button I created. Here's how it looks:
I am sorry if my code looks messy, I am a beginner.
Here is my code:
int x=75;
int lsW=17;
int lsH=50;
float bx =17;
float by = 25;
int boxSize = 50;
float xOffset = 0.0;
float yOffset = 0.0;
float sbx = 483;
float sby = 25;
int sboxSize = 50;
float sxOffset = 0.0;
float syOffset = 0.0;
void setup() {
size(550,300);
}
void draw() {
background(0);
fill (100);
rect (75, 25, 400, 50); //the slider track
if(mousePressed) {
if (mouseX >=75 && mouseX <= 475) //mousepress for the slider track
{x=mouseX;}
}
float value = map(x, 75, 475, 0, 100);
fill(255);
rect (x, 20, 3, 60); //the slider thumb, positioned at x}
fill (100);
if(mousePressed){
if (mouseX > bx-boxSize && mouseX < bx+boxSize &&
mouseY > by-boxSize && mouseY < by+boxSize){
if(x <=75){
x=76;}
fill(255);
if(x >=475){
x= x-40;
}else if (x >= 435){
x = x-40;
}
}
}
rect(bx, by, boxSize, boxSize);
fill(10);
triangle(30, 50, 50, 40, 50, 60);
fill(100);
if(mousePressed){
if (mouseX > sbx-sboxSize && mouseX < sbx+sboxSize &&
mouseY > sby-sboxSize && mouseY < sby+sboxSize){
if(x >= 475){
x=474;}
fill(255);
x++;
}
}
rect(483,25,50,50);
fill(10);
triangle(520, 50, 500, 40, 500, 60);
println(value);
line (115, 60, 115, 90);
line (155, 60, 155, 90);
line (195, 60, 195, 90);
line (235, 60, 235, 90);
line (275, 60, 275, 90);
line (315, 60, 315, 90);
line (355, 60, 355, 90);
line (395, 60, 395, 90);
line (435, 60, 435, 90);
}
I started trying to figure out how to make it go from 100 to 90 to 80 and so on. But I came across a problem wherein if I press the button, it goes to 80 instead of it stopping to 90 first. I am assuming this is cause by my else if (x >= 435){x = x-40;
statement. But I can't seem to figure out a way to fix this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
mousepressed()
并将滑块移至下一行按下按钮时:完成示例:
Use the
mousePressed()
and move the slider to the next line when the button is pressed:Complete example: