An array is a container that holds a fixed number of values, a bit like a spreadsheet table. With arrays we can store multiple variables of the same type, and access them easily.
...
Unlike arrays, ArrayLists can only be one dimension.
Exercise 8.2
Use an array or arraylist to draw animated icons on the screen.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
int noOfPuffs = 60; int[] puffsX = new int[noOfPuffs]; int[] puffsY = new int[noOfPuffs]; int puffSize= 40; int scaleDirection = 1; int currentPuff= 0; void setup() { size(600,600); noStroke(); } void draw() { background(0); for(int i = 0; i < puffsX.length; i++) { puffs(puffsX[i],puffsY[i]); } puffSize+=scaleDirection; if (puffSize>70 || puffSize<20) { scaleDirection = -scaleDirection; } } void puffs(int x, int y) { fill(255); ellipse(x,y,puffSize,puffSize); }; void mouseMoved(){ currentPuff++; if (currentPuff >= noOfPuffs) { currentPuff = 0; } puffsX[currentPuff] = mouseX; puffsY[currentPuff] = mouseY; }; |
...