在Processing.js中,如何声明对象类型和对象数组?

时间:2014-11-20 18:11:28

标签: javascript arrays object processing.js

我通过Kahn学院学习了processing.js,只需使用var即可声明任何类型的变量。但是,我发现使用varint这样的术语代替float而不是var。但是如何声明对象类型?而且,你如何声明一个对象数组?以下是在卡恩学院领域工作正常的相关代码。你能告诉我如何重写使用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}}

1 个答案:

答案 0 :(得分:0)

据我了解您的问题,您希望将基于JavaScript的代码从processing.js转换为基于Java的processing。你可以这样做:

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)));
}
相关问题