みーのぺーじ

みーが趣味でやっているPCやソフトウェアについて.Python, Javascript, Processing, Unityなど.

Processing.jsでArrayListを使う時の注意

Processing.js 1.4.1で検証しています.

ArrayListのコンストラクタで,キャパシティの初期値を設定すると,

Uncaught TypeError: Cannot read property "x" of undefined 

というエラーが表示されます.初期値なしだと正常に動作するみたいです.

つまり,下のスクリプトはjsでは動作しません.

ArrayList t;
void setup() {
  t = new ArrayList(10); // error
  t.add(new PVector(30,50));
  t.add(new PVector(30,70));
}

void draw() {
  background(192);
  for(int i=0;i<t.size();i++) {
    PVector p = (PVector)t.get(i);
    ellipse(p.x,p.y,50,50);
  }
}

ArrayList(10)の10を削除すると正常に動作します.

ArrayList t;
void setup() {
  t = new ArrayList(); // OK
  t.add(new PVector(30,50));
  t.add(new PVector(30,70));
}

void draw() {
  background(192);
  for(int i=0;i<t.size();i++) {
    PVector p = (PVector)t.get(i);
    ellipse(p.x,p.y,50,50);
  }
}

備忘録としてメモしておきます.