In Processing.js, how to declare an object type and an array of objects?

326 views Asked by At

I learned processing.js through Kahn academy, where you declare any type of variable simply by using var. However, I see that instead of var, terms like int and float are used. But how do you declare an object type? And, how do you declare an array of objects? Below is the relevant code that works fine in the the realm of Kahn Academy. Can you tell me how I would rewrite the lines that use var? Many thanks.

var Mover = function (xPos, yPos, cloudColor){
    this.xPos = xPos;
    this.yPos = yPos;
    this.cloudColor = cloudColor;
};


var movers = []; 

//I know the var below gets replaced with “int”:

for (var i = 0; i <=1000; i++){
    movers[i] = new Mover(random(-22,width), random(-22,height), random(188,255));
}
1

There are 1 answers

4
Ortomala Lokni On BEST ANSWER

As I understand your question, you want to translate your code from processing.js based on JavaScript to processing based on Java. You can do it like this:

class Mover{
  float xPos, yPos;
  float cloudColor;
  public Mover(float xPos, float yPos, float cloudColor){
    this.xPos = xPos;
    this.yPos = yPos;
    this.cloudColor = cloudColor; 
  }
}


ArrayList<Mover> movers = new ArrayList<Mover>(); 
for (int i = 0; i <1000; i++){
   movers.add(new Mover(random(-22,width),
                        random(-22,height),
                        random(188,255)));
}