// 円の生成のサンプル int MAX = 20; //円の最大値 int count = 0; //マウスクリックによって生まれる円の番号 //circlesオブジェクトを宣言 Circle[] circles = new Circle[MAX]; void setup() { size(300,300); smooth(); // circlesオブジェクトの生成と配列の初期化 for (int i = 0; i < MAX; i++) { circles[i] = new Circle(); } } void draw() { background(150); //常にcircles[]を更新 for (int i = 0; i < MAX; i++) { circles[i].update(); } } //マウスクリックによるイベント void mousePressed() { circles[count].x = mouseX; //circlesのx座標をマウスのxに変更 circles[count].y = mouseY; //circlesのy座標をマウスのyに変更 circles[count].radius = 30.0; //円の直径を設定 count ++; //順番に更新 if(count >= MAX) count = 0; } // Circle クラスを作成 ////////////////////////////////////////////////////////// class Circle { float x; //円のx座標 float y; //円のy座標 float radius; //円の直径 float xspeed; //x軸方向のスピード float yspeed; //y軸方向のスピード //初期化用のメソッド(コンストラクタ) Circle() { xspeed = random(-0.5,0.5); //スピードと方向をランダムに作成 yspeed = random(-0.5,0.5); //スピードと方向をランダムに作成 x = width/2; //x,yは画面の中心に設定 y = height/2; radius = 30.0; //円の直径を設定 } //円の描画更新用メソッド void update() { //円の直径をランダムに変化させる if (radius > 0) radius+=random(-1.0, 1.0); // speedで設定された値を足す x += xspeed; y += yspeed; noStroke(); fill(0); ellipse(x,y,radius,radius); //円を描画 } } //////////////////////////////////////////////////////////