为什么我的圈子显示为黑色?
Why are my circles displaying as black?
我正在尝试制作一个程序,在一个整齐的格子中创建最多 24 个圆圈,这些圆圈开始时为白色,然后在满足条件(例如锤子到达屏幕底部并重置)时,按从左到右的顺序更改这些圆圈 - 每次 x 轴圆圈到达其行的定义边缘时向下一行。
我创建了一个数组,在其索引中包含 24 种颜色,一开始全部设置为白色(- 与不可见圆圈的背景相同),但随着条件逐一改变。但是,当我 运行 下面的代码时,我从一开始就得到了一个不变的全黑圆圈的格子,没有考虑锤子,并且每七行的左上角圆圈中有一个小白点(仅一个在 6 行测试中可见 运行).
不知道是什么原因造成的,扫了无数次代码,根本看不出黑色是从哪里来的。 hammerPos if 语句此时不完整,因为我仍在尝试弄清楚发生了什么。
这是我的代码:
int hammerPos = 0; int hammerVel = 1; int hammerPending = 0;
int jump = 60;
int pointIntervX = 20;
int pointIntervY = 20;
int pointX = pointIntervX;
int pointY = pointIntervY;
int pointSize = 10;
color white = color(255,255,255);
color black = color(0,0,0);
color red = color(255,0,0);
color[] pointColour = new color[24];
//standalone method (at top outside of draw and setup)
void pointMaker(color colour, int pointX, int pointY){
fill(colour);
ellipse(pointX,pointY,pointSize,pointSize);
}
void setup(){
size(200,200);
background(255);
for(int i:pointColour){//setup pointColour array at BEGINNING of setup
pointColour[i] = white;
}
}
void draw(){
// draw hammer;
fill(0);
stroke(0);
strokeWeight(6);
line(128,hammerPos+10,148,hammerPos-12);
strokeWeight(4);
line(125,hammerPos+13,129,hammerPos+11);
strokeWeight(8);
line(115,hammerPos+5,130,hammerPos+20);
hammerPending = hammerPending + hammerVel;
if (hammerPending > jump){
hammerPos = hammerPos + hammerPending;
hammerPending = 0;
}
for(int i = 0;i<24;i++){//refreshes 24 circles in white(invisible) constantly in draw until a color is changed
pointMaker(pointColour[i], pointX, pointY);
pointX += pointIntervX;
if(pointX>pointIntervX*4){
pointX = pointIntervX;
if(pointY<pointIntervY*6){
pointY += pointIntervY;
}
}
}
if(hammerPos>height){
hammerPos = 0;
}
}
draw()
中的 for
循环在 strokeWeight(8)
仍然有效的上下文中执行。除了中风,你什么也看不到。一个简单的解决方法是将行 strokeWeight(1);
放在 pointMaker
.
定义的开头
当您这样做时,您会看到另一个错误。 for(int i:pointColour)
并没有按照您的想法行事。您正在迭代数组 pointColour
的默认 元素 (不是索引)——所有这些都是 0
。因此,您在该循环中所做的是连续 24 次将该数组的第一个元素设置为 white
,而使其他 23 个元素等于 0,即等于 black
。因此,在修复 strokeWeight
错误后,您会留下一个由 1 个白色圆圈和 23 个黑色圆圈组成的网格。您应该在创建数组时初始化数组,或者在 setup()
中使用 for(int i = 0; i < pointColour.length; i++) {
循环遍历索引而不是元素
On Edit:澄清索引与元素的含义:pointColour
被声明为 color[24]
。因此 for(int i: pointColour)
没有任何意义。使用这种类型的迭代,它应该是 for(color i: pointColour)
。您所写内容不是语法错误的唯一原因是 Processing 的颜色数据类型似乎是 32 位 int 的别名。 pointColour
中的索引始终是 24 个整数 0,1,...,23。 pointColour
的元素是您碰巧存储在该数组中的 24 种颜色。在 pointColour[2] = color(0,0,255)
中索引是 2 但元素是蓝色
我正在尝试制作一个程序,在一个整齐的格子中创建最多 24 个圆圈,这些圆圈开始时为白色,然后在满足条件(例如锤子到达屏幕底部并重置)时,按从左到右的顺序更改这些圆圈 - 每次 x 轴圆圈到达其行的定义边缘时向下一行。
我创建了一个数组,在其索引中包含 24 种颜色,一开始全部设置为白色(- 与不可见圆圈的背景相同),但随着条件逐一改变。但是,当我 运行 下面的代码时,我从一开始就得到了一个不变的全黑圆圈的格子,没有考虑锤子,并且每七行的左上角圆圈中有一个小白点(仅一个在 6 行测试中可见 运行).
不知道是什么原因造成的,扫了无数次代码,根本看不出黑色是从哪里来的。 hammerPos if 语句此时不完整,因为我仍在尝试弄清楚发生了什么。
这是我的代码:
int hammerPos = 0; int hammerVel = 1; int hammerPending = 0;
int jump = 60;
int pointIntervX = 20;
int pointIntervY = 20;
int pointX = pointIntervX;
int pointY = pointIntervY;
int pointSize = 10;
color white = color(255,255,255);
color black = color(0,0,0);
color red = color(255,0,0);
color[] pointColour = new color[24];
//standalone method (at top outside of draw and setup)
void pointMaker(color colour, int pointX, int pointY){
fill(colour);
ellipse(pointX,pointY,pointSize,pointSize);
}
void setup(){
size(200,200);
background(255);
for(int i:pointColour){//setup pointColour array at BEGINNING of setup
pointColour[i] = white;
}
}
void draw(){
// draw hammer;
fill(0);
stroke(0);
strokeWeight(6);
line(128,hammerPos+10,148,hammerPos-12);
strokeWeight(4);
line(125,hammerPos+13,129,hammerPos+11);
strokeWeight(8);
line(115,hammerPos+5,130,hammerPos+20);
hammerPending = hammerPending + hammerVel;
if (hammerPending > jump){
hammerPos = hammerPos + hammerPending;
hammerPending = 0;
}
for(int i = 0;i<24;i++){//refreshes 24 circles in white(invisible) constantly in draw until a color is changed
pointMaker(pointColour[i], pointX, pointY);
pointX += pointIntervX;
if(pointX>pointIntervX*4){
pointX = pointIntervX;
if(pointY<pointIntervY*6){
pointY += pointIntervY;
}
}
}
if(hammerPos>height){
hammerPos = 0;
}
}
draw()
中的 for
循环在 strokeWeight(8)
仍然有效的上下文中执行。除了中风,你什么也看不到。一个简单的解决方法是将行 strokeWeight(1);
放在 pointMaker
.
当您这样做时,您会看到另一个错误。 for(int i:pointColour)
并没有按照您的想法行事。您正在迭代数组 pointColour
的默认 元素 (不是索引)——所有这些都是 0
。因此,您在该循环中所做的是连续 24 次将该数组的第一个元素设置为 white
,而使其他 23 个元素等于 0,即等于 black
。因此,在修复 strokeWeight
错误后,您会留下一个由 1 个白色圆圈和 23 个黑色圆圈组成的网格。您应该在创建数组时初始化数组,或者在 setup()
中使用 for(int i = 0; i < pointColour.length; i++) {
On Edit:澄清索引与元素的含义:pointColour
被声明为 color[24]
。因此 for(int i: pointColour)
没有任何意义。使用这种类型的迭代,它应该是 for(color i: pointColour)
。您所写内容不是语法错误的唯一原因是 Processing 的颜色数据类型似乎是 32 位 int 的别名。 pointColour
中的索引始终是 24 个整数 0,1,...,23。 pointColour
的元素是您碰巧存储在该数组中的 24 种颜色。在 pointColour[2] = color(0,0,255)
中索引是 2 但元素是蓝色