我正在开展涉及屏幕的项目。我希望能够使用数字键到哪个屏幕,这将使用户进入草图的交互部分。
我开始在一个单独的草图中处理该程序的一个交互部分。这是草图:
float x, y, r, g, b, radius;
int timer;
void setup() {
size(500, 500);
background(255);
noStroke();
smooth();
}
void draw() {
Zon();
}
void Zon(){
// use frameCount to move x, use modulo to keep it within bounds
x = frameCount % width;
// use millis() and a timer to change the y every 2 seconds
if (millis() - timer >= 8000) {
y = random(height);
timer = millis();
}
// use frameCount and noise to change the red color component
r = noise(frameCount * 0.01) * 255;
// use frameCount and modulo to change the green color component
g = frameCount % 1;
// use frameCount and noise to change the blue color component
b = 255 - noise(1 + frameCount * 0.025) * 255;
// use frameCount and noise to change the radius
radius = noise(frameCount * 0.01) * mouseX;
color c = color(r, g, b);
fill(c);
ellipse(x, y, radius, radius);
}
这是单独草图的代码。我希望能够将这个草图放入我的实际项目中,但它不能像在单独的草图中那样运行。有人能解释一下为什么会这样吗?
我希望有一个白色背景,椭圆在屏幕上移动,留下一条痕迹。它不起作用,因为背景一遍又一遍地运行,擦除过程中的踪迹?
当我删除背景(255);它有点工作,除了它在菜单屏幕上运行,我不想要。
这里是实际的项目代码:
final int stateMenu = 0;
final int GreenBox = 3;
int state = stateMenu;
float x, y, r, g, b, radius;
int timer;
PFont font;
PFont Amatic;
void setup()
{
size(800, 700);
smooth();
font = createFont("ARCARTER-78.vlw", 14);
textFont(font);
//Amatic = createFont("Amatic-Bold.ttf",60);
//textFont(Amatic);
frameRate(15);
}
void draw()
{
// the main routine. It handels the states.
// runs again and again
switch (state) {
case stateMenu:
showMenu();
break;
case GreenBox:
handleGreenBox();
break;
default:
println ("Unknown state (in draw) "
+ state
+ " ++++++++++++++++++++++");
}
}
void keyPressed() {
// keyboard. Also different depending on the state.
switch (state) {
case stateMenu:
keyPressedForStateMenu();
break;
case GreenBox:
keyPressedForGreenBox();
}
}
void keyPressedForStateMenu() {
switch(key){
case '3':
state = GreenBox;
break;
default:
// do nothing
break;
}
}
void keyPressedForGreenBox(){
switch(key) {
default:
state = stateMenu;
break;
}
}
void showMenu() {
background(255);
fill(0);
textSize(45);
//textFont(Amatic);
text(" Music Box ", 330, 250, 3);
textSize(14);
text("Press 3 for Green", 350, 350);
}
void handleGreenBox() {
Zon();
}
void Zon(){
background(255);
noStroke();
smooth();
// use frameCount to move x, use modulo to keep it within bounds
x = frameCount % width;
// use millis() and a timer to change the y every 2 seconds
if (millis() - timer >= 8000) {
y = random(height);
timer = millis();
}
// use frameCount and noise to change the red color component
r = noise(frameCount * 0.01) * 255;
// use frameCount and modulo to change the green color component
g = frameCount % 1;
// use frameCount and noise to change the blue color component
b = 255 - noise(1 + frameCount * 0.025) * 255;
// use frameCount and noise to change the radius
radius = noise(frameCount * 0.01) * mouseX;
color c = color(r, g, b);
fill(c);
ellipse(x, y, radius, radius);
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
如果您希望能够绘制背景并拥有"跟踪"在那个背景上绘制,那么你有两个选择:
选项1:创建一个包含绘制所需内容的数据结构,然后每帧绘制该数据结构中的所有内容。这可能就像ArrayList<PVector>
一样简单,或者您可能最好创建自己的类,这些类封装了您需要知道的所有内容,以便在框架中绘制所有内容。
选项2:创建一个您绘制路径的PGraphics
。然后将背景绘制到屏幕上,然后将PGraphics
绘制到屏幕上。有关此方法的信息可以在the reference中找到。
你采取哪种方法取决于你。我建议整理一个小例子来测试每个例子,看看哪一个对你更有意义。如果你遇到困难,你可以发布MCVE这个小例子。祝你好运。
答案 1 :(得分:0)
在您的原始代码中,您没有清除后台,但在合并的代码版本中,您是:
void Zon(){
background(255);
...
并且你不希望如你所指出的那样。
唯一的另一个问题是,当您从菜单状态切换到GreenBox状态时,仍需要调用background(255)
,但只需调用一次:
void keyPressedForStateMenu() {
switch(key) {
case '3':
state = greenBox;
//clear the menu once when moving into greenBox
background(255);
break;
default:
// do nothing
break;
}
}
您在识别问题以及如何部分解决问题方面做得很好,只需在更改模式时清除背景。
您的代码如下所示:
final int stateMenu = 0;
final int greenBox = 3;
int state = stateMenu;
float x, y, r, g, b, radius;
int timer;
PFont font;
PFont Amatic;
void setup()
{
size(800, 700);
smooth();
//maybe loadFont ?
font = createFont("ARCARTER-78.vlw", 14);
textFont(font);
//Amatic = createFont("Amatic-Bold.ttf",60);
//textFont(Amatic);
frameRate(15);
}
void draw()
{
// the main routine. It handels the states.
// runs again and again
switch (state) {
case stateMenu:
showMenu();
break;
case greenBox:
handlegreenBox();
break;
default:
println ("Unknown state (in draw) "
+ state
+ " ++++++++++++++++++++++");
}
}
void keyPressed() {
// keyboard. Also different depending on the state.
switch (state) {
case stateMenu:
keyPressedForStateMenu();
break;
case greenBox:
keyPressedForgreenBox();
}
}
void keyPressedForStateMenu() {
switch(key) {
case '3':
state = greenBox;
//clear the menu once when moving into greenBox
background(255);
break;
default:
// do nothing
break;
}
}
void keyPressedForgreenBox() {
switch(key) {
default:
state = stateMenu;
break;
}
}
void showMenu() {
background(255);
fill(0);
textSize(45);
//textFont(Amatic);
text(" Music Box ", 330, 250);
textSize(14);
text("Press 3 for Green", 350, 350);
}
void handlegreenBox() {
Zon();
}
void Zon() {
//don't clear the buffer continuously if you want to leave trails
// background(255);
noStroke();
smooth();
// use frameCount to move x, use modulo to keep it within bounds
x = frameCount % width;
// use millis() and a timer to change the y every 2 seconds
if (millis() - timer >= 8000) {
y = random(height);
timer = millis();
}
// use frameCount and noise to change the red color component
r = noise(frameCount * 0.01) * 255;
// use frameCount and modulo to change the green color component
g = frameCount % 1;
// use frameCount and noise to change the blue color component
b = 255 - noise(1 + frameCount * 0.025) * 255;
// use frameCount and noise to change the radius
radius = noise(frameCount * 0.01) * mouseX;
color c = color(r, g, b);
fill(c);
ellipse(x, y, radius, radius);
}