在我的代碼中,我有一個類,Key它可以在隨機坐標處創建一個鍵。當一個鍵被按下時,該鍵被添加到一個ArrayList. 的ArrayList迭代中draw()的方法,和密鑰落在一定的流速??梢酝瑫r顯示多個鍵。我想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 回答

尚方寶劍之說
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
提交
取消