我通过 Kahn 学院学习了processing.js,在这里您只需使用 var
即可声明任何类型的变量。但是,我发现使用了诸如 int
和 float
之类的术语,而不是 var
。但是如何声明对象类型呢?并且,如何声明对象数组?下面是在卡恩学院领域运行良好的相关代码。你能告诉我如何重写使用 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));
}
最佳答案
据我了解您的问题,您想翻译 processing.js 中的代码基于 JavaScript 至 processing基于Java。你可以这样做:
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)));
}
关于javascript - 在Processing.js中,如何声明对象类型和对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27046587/