在我的代码中,我有一个类,Key它可以在随机坐标处创建一个键。当一个键被按下时,该键被添加到一个ArrayList. 的ArrayList迭代中draw()的方法,和密钥落在一定的流速。可以同时显示多个键。我想Key从ArrayList它离开屏幕视图后删除 a 。我尝试过类似的方法if (key.location.y - textAscent() > height) {keys.remove(key)},要么导致程序停止工作,要么导致字母停止移动但在到达屏幕底部时仍会显示。有什么建议么?编辑:停止工作,我的意思是程序冻结,我收到这个错误:java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909) at java.util.ArrayList$Itr.next(ArrayList.java:859) at FallingLetters.draw(FallingLetters.java:35) at processing.core.PApplet.handleDraw(PApplet.java:2426) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)我不知道该怎么办。PFont f;ArrayList<Key> keys;void setup() { fullScreen(1); f=createFont("Bahnschrift", 300, true); textFont(f); keys = new ArrayList();}void draw() { fill(#FF5254); rect(0, 0, width, height); for (Key k : keys) { k.display(); k.fall(); }}void keyPressed() { keys.add(new Key());}class Key { PVector location, velocity; char k; color c; public Key() { this.c = 0; this.k=key; this.location = new PVector(random(width), random(height)); this.velocity = new PVector(0, int(random(1, 11))); } void display() { fill(c); text(k, location.x, location.y); } void fall() { this.location.add(velocity); }}
1 回答
![?](http://img1.sycdn.imooc.com/545863f50001df1702200220-100-100.jpg)
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
卢克的回答已经很不错了,但我喜欢在简单的处理草图中采用的另一种方法是使用基本for循环并在列表上向后循环。
for(int i = keys.size()-1; i >= 0; i--){
Key key = keys.get(i);
key.display();
key.fall();
}
现在,如果您删除fall()函数内的一个键,您的循环将继续正常运行。
无耻的自我推销:这是一个关于 ArrayLists in Processing 的教程,包括我刚刚概述的方法。
添加回答
举报
0/150
提交
取消