亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

離開屏幕后如何銷毀對象?

離開屏幕后如何銷毀對象?

一只斗牛犬 2021-06-10 14:01:35
在我的代碼中,我有一個類,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 的教程,包括我剛剛概述的方法。


查看完整回答
反對 回復 2021-06-23
  • 1 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號