我正在尝试制作一个菜单,我想在按下或单击鼠标后触发一个事件。
当前,当我按下一个按钮时,它会触发一个事件,但是,我必须一直按下鼠标,否则它会停止。我知道有一个mouseClicked()
函数,但是在将其实现到我的代码中一直没有成功。
Button[] buts; // creating new instance of array
PImage img;
StringList inventory;
String item;
color rectColor = 80;
color rectHighlight = color(51);
color baseColor = color(107);
color currentColor = baseColor;
int rectX = 50;
int rectY = 50;
int awidth=277; int bheight=300; // creating my own width and height because my build in func return incorrect numbers.
int rectSize1=awidth/2;
int rectSize2=bheight/8;
boolean rectOver = false;
void setup() {
// img = loadImage("Ranma.jpg");
buts = new Button[4]; // allocating space in array
size(277, 300);
inventory = new StringList("New Game","Load Game","Options","Exit"); // initializing the array with text for my buttons
//initializeStars();
for ( int i= 0; i<4;i++){ // initializing buttons
buts[i] = new Button(rectX, rectY+(rectSize2+10)*i, rectSize1, rectSize2, rectColor, baseColor,rectHighlight,currentColor, rectOver); }
}
void draw() {
// image(img, 0, 0);
background(255);
for (int i= 0; i<4;i++){
buts[i].update(mouseX, mouseY);
if (buts[i].rectOver && mousePressed){ // I want to whenever the mouse is clicked on a button something happens, but not just while the mouse is pressed.
UIFUNC(i); }
buts[i].hover(); }
for (int i= 0; i<4;i++){ // text in in the end of loop, so showing ONTOP of squares instead of beneath.
item = inventory.get(i);
fill(255,0,0);
textSize(16);
text(item,rectX+awidth/10,rectY+(rectSize2+10)*i+30); }
}
class Button{
int rectX, rectY; // Position of square button
int rectSize; // Diameter of rect
color rectColor, baseColor;
color rectHighlight;
color currentColor;
boolean rectOver; // must be set to false
String item;
int rectSize1;
int rectSize2;
Button(int tempRectX, int tempRectY, int tempRectsize1,int tempRectsize2 ,color tempRectcolor, color tempBasecolor,color tempRecthighlight,color tempCurrentcolor, boolean tempRectover){
rectX =tempRectX;
rectY =tempRectY;
rectSize1 =tempRectsize1;
rectSize2 =tempRectsize2;
rectColor =tempRectcolor;
baseColor = tempBasecolor;
rectHighlight= tempRecthighlight;
currentColor = tempCurrentcolor;
rectOver = tempRectover;
}
void update(int x, int y) { //if the mouse(for instance) is on the rectange/button then the boolean becomes true
if ( rectOver(rectX, rectY, rectSize1, rectSize2) ) {
rectOver = true;
}
else {
rectOver = false;}
}
boolean rectOver(int x, int y, int width1, int height1) { // is the mouse curser above the button
if (mouseX >= x && mouseX <= x+width1 &&
mouseY >= y && mouseY <= y+height1) {
return true;
} else {
return false;
}
}
void hover(){ // draws the buttons and highlights the buttons if mouse cursor is above.
if (rectOver) {
fill(rectHighlight,125);
} else {
fill(rectColor,125);
}
stroke(255);
rect(rectX, rectY, rectSize1, rectSize2);
}
}
// my tempory and very bad attempt at making my buttons do stuff.
int i;
void UIFUNC(int i){
if (i==0){
textSize(32);
text("Loading a saved game",rectX,rectY);
// drawStars();
}
else if (i==1){
textSize(32);
text("Loading a saved game",rectX,rectY);}
else if (i==2){
textSize(32);
text("options",rectX,rectY);}
else if (i==3){
exit();
}
}
非常感谢您的宝贵时间! 最佳马克
答案 0 :(得分:0)
您必须添加一个状态变量(clicked_button
),该状态变量说明被单击的按钮的索引。
在mouseClicked()
中设置状态:
int clicked_button = -1;
void mouseClicked() {
for (int i= 0; i<4; i++) {
if (buts[i].rectOver) {
clicked_button = i;
}
}
}
如果声明了变量(if (clicked_button >= 0)
),则处理按钮通知,并在clicked_button = -1;
中重置状态(draw()
):
void draw() {
// image(img, 0, 0);
background(255);
for (int i= 0; i<4;i++){
buts[i].update(mouseX, mouseY);
buts[i].hover();
}
// I want to whenever the mouse is clicked on a button something happens,
// but not just while the mouse is pressed.
if (clicked_button >= 0) {
UIFUNC(clicked_button);
clicked_button = -1;
}
for (int i= 0; i<4;i++){
item = inventory.get(i);
fill(255,0,0);
textSize(16);
text(item,rectX+awidth/10,rectY+(rectSize2+10)*i+30); }
}