因此,我有一个字符串数组,通过 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>
第一个代码在鼠标所在的位置绘制一个圆圈,并且它不会清除旧框架,因此您可以看到所有先前绘制的圆圈。第二个代码在鼠标所在的位置绘制一个圆圈,但它会清除旧框架,因此您只能看到最近绘制的圆圈。
您想做一些类似的事情来清除旧框架。更多信息可参见the reference .
关于java - 隐藏先前显示的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47662328/