[英]Hiding previously displayed array elements
因此,我有一个字符串数组,可以通过text()
函数一次显示在屏幕上。 但是,当我添加到索引中时,先前的数组元素仍停留在屏幕上,我无法弄清楚如何删除它。 我只想滚动浏览屏幕上的文本,并本质上让下一个数组元素替换它在屏幕上。
Dialog[] main = new Dialog[12];
int index;
void setup() {
for (int index = 0; index < main.length; index++) {
main[index] = new Dialog();
}
void draw() {
for (int index = 0; index < main.length; index++) {
main[index].readDialog();
}
}
if (mousePressed) {
index++:
}
String[] textScroll = new String[12];
class Dialog {
Dialog () {
textScroll[0] = "Welcome to the game!";
textScroll[1] = "Are you ready to play?";
etc...
}
void readDialog() {
text(textScroll[index], width/2, 100);
}
}
首先,您的语法无效。 您缺少大括号,并且在函数外部有if
语句,该语句无效。 请尝试发布具有有效语法的MCVE ,而不是未连接的代码段。
但是要回答您的一般问题,您可以通过调用background()
函数清除旧框架。 比较这段代码:
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.js"></script> <script type="application/processing"> void draw(){ ellipse(mouseX, mouseY, 20, 20); } </script> <canvas> </canvas>
此代码:
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.js"></script> <script type="application/processing"> void draw(){ background(32); ellipse(mouseX, mouseY, 20, 20); } </script> <canvas> </canvas>
第一个代码无论鼠标位于何处都将绘制一个圆圈,并且不会清除旧框架,因此您可以看到所有先前绘制的圆圈。 第二个代码无论鼠标在哪里都画一个圆,但是它清除了旧框架,因此您只能看到最近绘制的圆。
您想要执行类似的操作以清除旧框架。 在参考资料中可以找到更多信息。
要么使用background();
以您想要的颜色作为背景色作为参数。 例如background(0,0,100);
是蓝色背景。 这将在每个draw()
循环的开始清除屏幕。 但是,如果您不想每次在程序循环通过draw()
清除屏幕上的所有内容,只需在文本顶部使用与背景相同颜色的rect()
覆盖您的私人文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.