我使用以下代码试用canvas
元素
Shape = function(x,y){
this.x = x;
this.y = y;
};
shapes = new array();
shapes.push(new Shape(50,50,10,10));
shapes.push(new Shape(100,100,10,10));
shapes.push(new Shape(150,150,10,10));
function animate(){
context.clearRect(0,0, canvas.width(),canvas.height());
var shapesLength = shapes.length;
for(var i = 0; i < shapesLength; i++){
var tmpShape = shapes[i];
tmpShape.x++;
context.fillRect(tmpShape.x,thmpShape.y,10,10);
}
context.fillStyle = 'yellow';
context.fill();
if(playAnimation){
setTimeout(animate, 1)
}
}
但是,当我在浏览器中运行它时,我收到以下错误 -
ReferenceError: array is not defined
shapes = new array();
我试过把它变成一个全局和局部变量,我只是看不出我哪里出错了?
答案 0 :(得分:3)
array
应该大写,因为这是构造函数的名称:
shapes = new Array();
此外,最好使用方括号表示法来创建数组。像这样:
shapes = [];