(语言正在处理) 嘿,我有一项我不太懂的任务。这是另一个赋值的第2部分,我必须创建一个名为drawButton的函数,它使用数组作为我们正在绘制的按钮的参数。总共有3个具有预定值(x,y,大小,颜色)的按钮这里是第2部分;
编写另一个名为的函数定义 isPositionWithinButton。功能应该 取三个参数:一个点(x,y)和一个按钮 阵列。在测试的函数中实现代码 这点是否在范围内 按钮并使该函数返回true或false 取决于结果。调用该函数 从代码中的适当位置(例如使用 mouseX和mouseY作为点参数)和 打印button1,-2或-3基于传递 参数和返回的值。
所以,我现在所处的位置是我已经完成了这个功能,它可以说你输入的坐标是否在按钮内,但是我不确定最后一部分是什么,我必须做。 我的猜测是,如果坐标在按钮坐标内,我必须以某种方式使程序绘制按钮?无论如何,我不知道该怎么做,所以非常感谢帮助!
这是我到目前为止的地方:
int[] button1;
int[] button2;
int[] button3;
boolean within;
void setup(){
size(800,800);
int[] button1 = {75,250,200,200,150,160,170};
int[] button2 = {315,250,200,200,150,160,170};
int[] button3 = {550,250,200,200,150,160,170};
isPositionWithinButton(100,280,button1);
}
void drawButton(int[] buttonArray) {
fill(buttonArray[4],buttonArray[5],buttonArray[6]);
rect(buttonArray[0],buttonArray[1],buttonArray[2],buttonArray[3]);
}
void isPositionWithinButton(int x, int y, int[] buttonArray){
if (x>buttonArray[0] && x<buttonArray[0]+buttonArray[2] && y>buttonArray[1] && y<buttonArray[1]+buttonArray[2]){
within=true;
}
else{
within=false;
}
if (within==true){
println("Within.");
}
else{
println("Not within.");
}
}
答案 0 :(得分:1)
我调整了你的代码来完成这些事情。阅读评论以了解更改。 我没有使用循环来保持事物尽可能接近你的代码。希望有所帮助。
//read below..
int[] button1 = {75,250,200,200,150,160,170};
int[] button2 = {315,250,200,200,150,160,170};
int[] button3 = {550,250,200,200,150,160,170};
void setup(){
size(800,800);
// by repeating the 'int[]' part here you were
// shadowing, redeclaring, the global vars
// leaving those unitialized.
// you could just delete the 'int[]' part, but then the direct
// assining won't work. You would need to do:
//button1[0] = 75;
//button1[1] = 250 and so on...
// so I just moved the decaration to global scope
//int[] button1 = {75,250,200,200,150,160,170};
//int[] button2 = {315,250,200,200,150,160,170};
//int[] button3 = {550,250,200,200,150,160,170};
//calling your draw function
drawButton(button1);
drawButton(button2);
drawButton(button3);
}
// needed to call draw so skecth keeps running and you can test mouse coordinates
void draw(){
//now that the function has a return type boolean
// you can use it like this
if(isPositionWithinButton(mouseX, mouseY, button1)){
println("hello, this is button 1");
}
if(isPositionWithinButton(mouseX, mouseY, button2)){
println("hello, this is button 2");
}
if(isPositionWithinButton(mouseX, mouseY, button3)){
println("hello, this is button 3");
}
}
void drawButton(int[] buttonArray) {
fill(buttonArray[4],buttonArray[5],buttonArray[6]);
rect(buttonArray[0],buttonArray[1],buttonArray[2],buttonArray[3]);
}
// "make the function return true or false"
// A function that returns something is not void
// you use the returned type instead, here boolean
// So you test and return either true or false
boolean isPositionWithinButton(int x, int y, int[] buttonArray){
if (x>buttonArray[0] && x<buttonArray[0]+buttonArray[2] && y>buttonArray[1] && y<buttonArray[1]+buttonArray[2]){
return true;
}
else{
return false;
}
// all this body above could be repalced by this line
//return x>buttonArray[0] && x<buttonArray[0]+buttonArray[2] && y>buttonArray[1] && y<buttonArray[1]+buttonArray[2];
// It says like... return true if following condition… else return false
}